Solved

Calling pointers to class methods

Posted on 2003-10-23
12
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
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.

628 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