Link to home
Start Free TrialLog in
Avatar of chutney_man
chutney_man

asked on

Calling pointers to class methods

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?

Avatar of jkr
jkr
Flag of Germany image

You have to use

Thing* p = this;

p->f(nSomeInt, &vpSomePointer);  
Avatar of chutney_man
chutney_man

ASKER

That doesn't work ;-)
Try the following:
 (f)(nSomeInt, &vpSomePointer);
Just checked the places where I used contructs like that:

(p->f)(nSomeInt, &vpSomePointer);
The following should work:

       ((*this).*f)(nSomeInt, &vpSomePointer);
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.
>>(this->*f)(nSomeInt, &vpSomePointer);

Not with VC6 :o)
>>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.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahh thanks guys. (this->*f) did it......  and yes it does work in VC6  :)

Cripes, I should have thought of that too :P
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