Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi, I did some research here on function pointers
on class member functions, it seems if one
wants to use function pointers to class member
functions, we can't avoid wrapper methods right?
(i.e., who embed the actual function we want to call through function pointers).
Posted 24-Feb-13 19:55pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your research was not very thorough then. Of course you can call a member function via a member function pointer without using a static wrapper. You just have to use the correct syntax and apply the call to an object of the corresponding type. So you have to keep two things around:
 
- the object pointer
- the member function pointer
 
Many people try to combine both things into what is called a Delegate. If you search under this keyword, you will find a great many CodeProject articles that explain how to implement delegates in C++.
 
But perhaps get familiar with the syntax of calling a function via a member function pointer first.
  Permalink  
Comments
Matra Divat at 25-Feb-13 4:20am
   
Yes, I am thinking now...
I think in my class I may need a function pointer to a member function of ANY class...
 
and this: "-the member function pointer" as you mention, I think would require me to indicate in the class where I need function pointers the class name too -- which as I mentioned above might not be what I want ...
nv3 at 25-Feb-13 4:46am
   
Perhaps you should explain what you are trying to accomplish.
Matra Divat at 25-Feb-13 4:49am
   
Well I have a class that may store as a callback a pointer to a function which can be a member of ANY class I suppose...
nv3 at 25-Feb-13 5:15am
   
And here is the difference between a plain function pointer and a member function pointer. A member function pointer must always be applied to a particular object. You cannot simply call a member function without saying to which class object it is going to be applied.
Matra Divat at 25-Feb-13 5:35am
   
yes and that's why I think I can't avoid using wrapper functions... thanks
nv3 at 25-Feb-13 5:50am
   
No! Even if you use a wrapper function you have to pass a pointer to the target object as a parameter to that function. So you need that object pointer in any case. And using a wrapper function you are no longer free to call any arbitrary member of a class.
 
Note that casting a member function pointer of class X into a member function pointer of class Y is generally not a legal operation in C++. The reason is that member function pointers may have different implementation size (some 4 to 16 bytes) depending on the class characteristics of the target class. So beware of careless casting a member function pointer!
 
Matra Divat at 25-Feb-13 5:58am
   
Hi, I don't think I am using casting. Here is my class A which needs callbacks from different other classes, here how the declaration of the function pointer looks like:
void (*ptCallBack)(void* pt2Object, ResponseObject *genericResponse);
Now, ptCallBack can point to any Static function of ANY class right? So, inside that static function I can call the member function I need using pt2Object (as you mentioned also; e.g., pt2Object->MyMethod()). What is the problem with it? Or you think there is better way to do it?? Thanks.
nv3 at 25-Feb-13 7:01am
   
In that case the cast from void* to your object is legal. But you are limited to call a particular function from inside you static function. So, for every member function you want to call you need a static wrapper. If you can live with that restriction, then your method works well.
 
Nevertheless, take a look at delegates and how they make things easier for you.
Stefan_Lang at 25-Feb-13 6:53am
   
Callback mechanisms don't require function pointers at all. It's easy to implement using polymorphism and the correct design pattern.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 682
1 ChauhanAjay 135
2 Maciej Los 130
3 ChintanShukla 90
4 ashok rathod 90
0 Sergey Alexandrovich Kryukov 10,200
1 OriginalGriff 6,987
2 CPallini 5,850
3 George Jonsson 4,015
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100