Click here to Skip to main content
11,639,178 members (63,044 online)
Click here to Skip to main content

Tagged as

Invoke method pointers elegantly in C++

, 15 Jul 2012 CPOL 8.8K 2
Rate this:
Please Sign up or sign in to vote.
invoke method pointer elegantly

Introduction

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

Code

//
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; 
 };    
 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
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

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 
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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 16 Jul 2012
Article Copyright 2012 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid