Solved

Calling pointers to class methods

Posted on 2003-10-23
12
176 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now