Solved

Calling pointers to class methods

Posted on 2003-10-23
12
208 Views
Last Modified: 2010-04-02
I'm trying to call a pointer to a class method with the compiler spitting out: "term does not evaluate to a function"

This is basically what I'm trying to implement:

class Thing;
typedef long(Thing::*FUNC)(int, void**);

class Thing {
   Thing() { /*code & other junk */ }
    long someMethod(int n, void **ppv) { /* code */}
 
    void doIt(void {
       FUNC f = someMethod;    //This is legal..... (I'm actually using an array of pointers in my code....)

        /* more code */

        f(nSomeInt, &vpSomePointer);  //craps out here..... "term does not evaluate to a function"

    }

}

this snippet is quite lacking but it shows what i mean. This works fine with a regular static function. Why not with an instance method?

0
Comment
Question by:chutney_man
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9608742
You have to use

Thing* p = this;

p->f(nSomeInt, &vpSomePointer);  
0
 

Author Comment

by:chutney_man
ID: 9609050
That doesn't work ;-)
0
 
LVL 30

Expert Comment

by:Axter
ID: 9609078
Try the following:
 (f)(nSomeInt, &vpSomePointer);
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 9609121
Just checked the places where I used contructs like that:

(p->f)(nSomeInt, &vpSomePointer);
0
 
LVL 30

Expert Comment

by:Axter
ID: 9609144
The following should work:

       ((*this).*f)(nSomeInt, &vpSomePointer);
0
 
LVL 30

Expert Comment

by:Axter
ID: 9609182
Both of the following syntax will work.

(this->*f)(nSomeInt, &vpSomePointer);

((*this).*f)(nSomeInt, &vpSomePointer);

The main thing is that you need to use the * charactor before the "f" function variable, and you have to put brackets around the entire function pointer syntax.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9609273
>>(this->*f)(nSomeInt, &vpSomePointer);

Not with VC6 :o)
0
 
LVL 30

Expert Comment

by:Axter
ID: 9609316
>>Not with VC6 :o)

Yes, with VC6.  I tested it with VC6 before I posted.
Even though I have VC 7.1, I still do most of my testing on VC6.0 since I see it as the lowest denominator.
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 9609331
Here's a full example you can compile on VC6.0:

class Thing;
typedef long(Thing::*FUNC1)(int, void**);

class Thing {
public:
   long someMethod_non_static(int n, void **ppv) { return 3;}

    void doIt(void) {
      FUNC1 f_non_static = someMethod_non_static;
       ((*this).*f_non_static)(3, NULL);
       (this->*f_non_static)(3, NULL);

   }
};

int main(int argc, char* argv[])
{
      Thing MyThing;
      MyThing.doIt();



jkr,
Just out of curiousity, do you use www.experts-exchange.com, or are you using oldlook.experts-exchange.com?
0
 

Author Comment

by:chutney_man
ID: 9609338
ahh thanks guys. (this->*f) did it......  and yes it does work in VC6  :)

Cripes, I should have thought of that too :P
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546083
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Axter {http:#9609331}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question