Solved

Calling pointers to class methods

Posted on 2003-10-23
12
199 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 128
Lambda for random numbers problem 7 113
thread-safe code in c++ 2 104
Problem with SqlConnection 4 171
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

832 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