Click here to Skip to main content
15,174,547 members
Articles / Programming Languages / C++
Tip/Trick
Posted 15 Jul 2012

Tagged as

Stats

17.7K views
2 bookmarked

Invoke method pointers elegantly in C++

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
15 Jul 2012CPOL
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)

Share

About the Author

Mukit, Ataul
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 11:13
Memberpasztorpisti26-Jul-12 11:13 
AnswerRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 8:10
MemberMukit, Ataul27-Jul-12 8:10 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti27-Jul-12 12:38
Memberpasztorpisti27-Jul-12 12:38 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 19:58
MemberMukit, Ataul27-Jul-12 19:58 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti28-Jul-12 0:38
Memberpasztorpisti28-Jul-12 0: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 6:56
MemberMukit, Ataul28-Jul-12 6:56 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti28-Jul-12 14:57
Memberpasztorpisti28-Jul-12 14:57 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul28-Jul-12 19:40
MemberMukit, Ataul28-Jul-12 19:40 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 2:34
Memberpasztorpisti29-Jul-12 2:34 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul29-Jul-12 3:11
MemberMukit, Ataul29-Jul-12 3:11 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 3:30
Memberpasztorpisti29-Jul-12 3:30 
GeneralMy vote of 1 Pin
Christian Brüggemann17-Jul-12 0:55
MemberChristian Brüggemann17-Jul-12 0:55 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 22:18
MemberMukit, Ataul20-Jul-12 22:18 
QuestionHow... Pin
Ollie C16-Jul-12 0:13
MemberOllie C16-Jul-12 0:13 
AnswerRe: How... Pin
Mukit, Ataul20-Jul-12 22:27
MemberMukit, Ataul20-Jul-12 22:27 
GeneralMy vote of 1 Pin
Mike Diack15-Jul-12 23:42
MemberMike Diack15-Jul-12 23:42 
GeneralRe: My vote of 1 Pin
Zaraken16-Jul-12 3:50
MemberZaraken16-Jul-12 3:50 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 22:22
MemberMukit, Ataul20-Jul-12 22:22 
QuestionIt's no good just posting source code with no commentary! Pin
Mike Diack15-Jul-12 23:41
MemberMike Diack15-Jul-12 23: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.