Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling pointers to class methods

Posted on 2003-10-23
12
Medium Priority
?
220 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
11 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

783 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