www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - pointer to a class member

↑ ↓ ← 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
↑ ↓ → 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