"OvErboRed" <pu******@SPAMoverbored.net> wrote in message
news:Xn******************************@127.0.0.1...
I know you can do:
int (MyClass::*ptr)(int) = &MyClass::func;
cout << (myObj.*ptr)(5);
to point to a member function of class MyClass. But how would you go about
making this generic (i.e., not restricted to any specific class, like
MyClass)? You can probably resort to C function pointers, but I'd rather
not. Thanks in advance.
You can't. You can try cheating, if you don't mind writing non-C++,
non-portable code that might stop working on a different compiler, or the
next version of your current compiler, or might never work at all on certain
classes, depending on how the compiler implements them. I once had to work
on a large, complex and highly event-driven application. The compiler at the
time didn't have templates. I decided that the usefulness of such a generic
pointer outweighed language and portability issues, and I wrote some nasty
macros that enabled any class to have a member-function event handler. The
event manager thought it was always calling a member of class EventHandler,
which was defined as follows:
class EventHandler
{
};
In fact, it was calling member functions of a large number of different,
unrelated classes at different times from the same call statement.
DW