Click here to Skip to main content
Click here to Skip to main content

Tagged as

Invoke method pointers elegantly in C++

, 15 Jul 2012 CPOL
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
C++ is not C with classes, JQuery is not Javascript, Google Search is not Learning, Design Patterns are not fashion, A code written in 2005 is not backdated just because it's 2015
Follow on   Twitter

Comments and Discussions

 
QuestionCan't escape dirty code when its about member pointers! Pinmemberpasztorpisti26-Jul-12 11:13 
AnswerRe: Can't escape dirty code when its about member pointers! PinmemberMukit, Ataul27-Jul-12 8:10 
GeneralRe: Can't escape dirty code when its about member pointers! Pinmemberpasztorpisti27-Jul-12 12:38 
GeneralRe: Can't escape dirty code when its about member pointers! [modified] PinmemberMukit, Ataul27-Jul-12 19:58 
GeneralRe: Can't escape dirty code when its about member pointers! Pinmemberpasztorpisti28-Jul-12 0:38 
GeneralRe: Can't escape dirty code when its about member pointers! PinmemberMukit, Ataul28-Jul-12 6:56 
GeneralRe: Can't escape dirty code when its about member pointers! Pinmemberpasztorpisti28-Jul-12 14:57 
GeneralRe: Can't escape dirty code when its about member pointers! PinmemberMukit, Ataul28-Jul-12 19:40 
GeneralRe: Can't escape dirty code when its about member pointers! Pinmemberpasztorpisti29-Jul-12 2:34 
GeneralRe: Can't escape dirty code when its about member pointers! PinmemberMukit, Ataul29-Jul-12 3:11 
GeneralRe: Can't escape dirty code when its about member pointers! Pinmemberpasztorpisti29-Jul-12 3:30 
GeneralMy vote of 1 PinmemberChristian Brüggemann17-Jul-12 0:55 
GeneralRe: My vote of 1 PinmemberMukit, Ataul20-Jul-12 22:18 
QuestionHow... PinmemberOllie C16-Jul-12 0:13 
AnswerRe: How... PinmemberMukit, Ataul20-Jul-12 22:27 
GeneralMy vote of 1 PinmemberMike Diack15-Jul-12 23:42 
GeneralRe: My vote of 1 PinmemberZaraken16-Jul-12 3:50 
GeneralRe: My vote of 1 PinmemberMukit, Ataul20-Jul-12 22:22 
QuestionIt's no good just posting source code with no commentary! PinmemberMike Diack15-Jul-12 23: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
Web01 | 2.8.1411022.1 | Last Updated 16 Jul 2012
Article Copyright 2012 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid