www.digitalmars.com         C & C++   DMDScript  

c++ - pointer to a class member

reply vicentico1 <vicentico1_member pathlink.com> writes:
how do i define a pointer-to-member for any class? what i want is to make a
typedef of a member function of any class.

something like 
typedef int(*name)(int,int);

class a { public: int aa(int,int) } classa;
class b { public: int ba(int,int) } classb;

and then passit to a function like this

name var = classa.aa, var2 = classsb.ba;

is this possible??


vicentico
May 29 2003
parent Richard Grant <fractal clark.net> writes:
In article <bb5c6q$uqp$1 digitaldaemon.com>, vicentico1 says...
how do i define a pointer-to-member for any class? what i want is to make a
typedef of a member function of any class.
class a { public: int plus(int i,int j) { return i + j; } } classa; class b { public: int multiply(int i,int j) { return i * j; } } classb; template <class T> struct functor { functor(T* t, int (T::*fn)(int, int)) : m_(t), m_f(fn) {} int operator()(int lhs, int rhs) { return (*m_.*m_f)(lhs, rhs); } private: T* m_; int (T::*m_f)(int, int); }; int main() { functor<a> fa(&classa, classa.plus); functor<b> fb(&classb, classb.multiply); std::cout << fa(1000, -2000) << std::endl; std::cout << fb(70, 54); } Richard
May 29 2003