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

Tagged as

Stats

18.2K 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 10:13
Memberpasztorpisti26-Jul-12 10:13 
AnswerRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 7:10
MemberMukit, Ataul27-Jul-12 7:10 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti27-Jul-12 11:38
Memberpasztorpisti27-Jul-12 11:38 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul27-Jul-12 18:58
MemberMukit, Ataul27-Jul-12 18:58 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti27-Jul-12 23:38
Memberpasztorpisti27-Jul-12 23:38 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul28-Jul-12 5:56
MemberMukit, Ataul28-Jul-12 5:56 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti28-Jul-12 13:57
Memberpasztorpisti28-Jul-12 13:57 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul28-Jul-12 18:40
MemberMukit, Ataul28-Jul-12 18:40 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 1:34
Memberpasztorpisti29-Jul-12 1:34 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
Mukit, Ataul29-Jul-12 2:11
MemberMukit, Ataul29-Jul-12 2:11 
GeneralRe: Can't escape dirty code when its about member pointers! Pin
pasztorpisti29-Jul-12 2:30
Memberpasztorpisti29-Jul-12 2:30 
Well, actually I don't exactly know the class and the problem but I hardly believe it cant be solved without hiding the method pointers from your own code. If you have a fixed number of methods inside the class, then you can define an enum for it and set that from your code and if the sort methods are in the same class then the pointer inside your delegate is needless because every sorting call will come from the same object so you will have a this pointer from somewhere. If I haven't misinterpreted the situation then all you need is an enum in the class that has a member for each sort method plus a set method that changes this enum. Maybe this same set method could store and change a method pointer instead of the enum itself.
GeneralMy vote of 1 Pin
Christian Brüggemann16-Jul-12 23:55
MemberChristian Brüggemann16-Jul-12 23:55 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 21:18
MemberMukit, Ataul20-Jul-12 21:18 
QuestionHow... Pin
Ollie C15-Jul-12 23:13
MemberOllie C15-Jul-12 23:13 
AnswerRe: How... Pin
Mukit, Ataul20-Jul-12 21:27
MemberMukit, Ataul20-Jul-12 21:27 
GeneralMy vote of 1 Pin
Mike Diack15-Jul-12 22:42
MemberMike Diack15-Jul-12 22:42 
GeneralRe: My vote of 1 Pin
Zaraken16-Jul-12 2:50
MemberZaraken16-Jul-12 2:50 
GeneralRe: My vote of 1 Pin
Mukit, Ataul20-Jul-12 21:22
MemberMukit, Ataul20-Jul-12 21:22 
QuestionIt's no good just posting source code with no commentary! Pin
Mike Diack15-Jul-12 22:41
MemberMike 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.