Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C++
Tip/Trick

Invoke method pointers elegantly in C++

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
15 Jul 2012CPOL 21.4K   2   19
invoke method pointer elegantly

Introduction

This tip shows an elegent way to invoke method pointers in C++.

Code

C++
//
template <class T> 
class MemFnPtr 
{  
public: 
   MemFnPtr(int (T::*fp)(int), T &t) : m_fp(fp), m_t(t)
   { 
   } 
   int Fn(int n) 
   { 
      return ((m_t).*(m_fp))(n); 
   }
   //member vars 
   int (T::*m_fp)(int); 
   T &m_t; 
 };    
C++
 int main()
   { 
   A a_obj; 
   MemFnPtr<A> fp_a(&A::Func1, a_obj); 
   cout << fp_a.Fn(10) << endl; 
   return 0; 
   } 

History

Tip submitted : 16th July, 2012

License

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


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

 
QuestionCan't escape dirty code when its about member pointers! Pin
pasztorpisti26-Jul-12 10:13
pasztorpisti26-Jul-12 10:13 
AnswerRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 7:10
Mukit, Ataul27-Jul-12 7:10 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti27-Jul-12 11:38
pasztorpisti27-Jul-12 11:38 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 18:58
Mukit, Ataul27-Jul-12 18:58 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti27-Jul-12 23:38
pasztorpisti27-Jul-12 23:38 
If I have to use ugly library then I create a facade - a class that shows a nice interface to me and has the cr@p inside. In this case I don't mind member pointers inside but its rare to see member pointers even in the ugliest libraries outside.
Unfortunately I cant recall a situation where your code makes a big enough difference because of the rarity of member pointers. If I want to call a method with member pointer like you did, then I can wrap this call inside a method of my facade class, no need for template magic here.
I have seen member pointers with reason only in event handlers (UI code - I usually use weak pointed listeners instead), and in a serialization framework where the framework stored the data member offsets as member pointers. Usually when some libraries use member pointers the situation becomes ugly when they start to cast the member pointer and when it comes to crossplatform - for example in gcc a member pointer is 4 bytes or 8 bytes if the class has multiple inheritance somewhere in the hierarchy. The resulting code becomes the hardest to maintain mess.
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul28-Jul-12 5:56
Mukit, Ataul28-Jul-12 5:56 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti28-Jul-12 13:57
pasztorpisti28-Jul-12 13:57 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul28-Jul-12 18:40
Mukit, Ataul28-Jul-12 18:40 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 1:34
pasztorpisti29-Jul-12 1:34 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul29-Jul-12 2:11
Mukit, Ataul29-Jul-12 2:11 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 2:30
pasztorpisti29-Jul-12 2:30 
GeneralMy vote of 1 Pin
Christian Brüggemann16-Jul-12 23:55
Christian Brüggemann16-Jul-12 23:55 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 21:18
Mukit, Ataul20-Jul-12 21:18 
QuestionHow... Pin
Ollie C15-Jul-12 23:13
Ollie C15-Jul-12 23:13 
AnswerRe: How... Pin
Mukit, Ataul20-Jul-12 21:27
Mukit, Ataul20-Jul-12 21:27 
GeneralMy vote of 1 Pin
Mike Diack15-Jul-12 22:42
Mike Diack15-Jul-12 22:42 
GeneralRe: My vote of 1 Pin
Zaraken16-Jul-12 2:50
Zaraken16-Jul-12 2:50 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 21:22
Mukit, Ataul20-Jul-12 21:22 
QuestionIt's no good just posting source code with no commentary! Pin
Mike Diack15-Jul-12 22:41
Mike Diack15-Jul-12 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.