Function Pointers To Member Functions

O

OvErboRed

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.
 
V

Victor Bazarov

OvErboRed said:
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 cannot make it generic. Pointers to members are class-specific.
Period.

Victor
 
O

OvErboRed

Fine, but is there no other way to pass in member functions as general
callbacks? (I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)
 
V

Victor Bazarov

OvErboRed said:
Fine, but is there no other way to pass in member functions as general
callbacks?

Usually not. See FAQ.
(I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)

My guess is that it's possible, but you'll be involving templates and
thus mostly giving up run-time choices.
 
D

David White

OvErboRed said:
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
 
B

Bernhard Holzmayer

OvErboRed said:
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.

If this is a way, use a base class which all classes inherit of.
Virtual methods will grant you access to the methods of the derived
classes, too.

Bernhard
 
I

Ivan Vecerina

NB: OvErboRed, please do not top-post (I moved your reply down).
OvErboRed said:
Fine, but is there no other way to pass in member functions as general
callbacks? (I guess I'm basically trying to get the same effect as
runnables in Java or delegates in C#, but using function pointers instead
of classes implementing Runnable or IDelegate.)

Such a 'generalized callback' can be implemented as a library in C++.
boost.org, a peer-reviewed collection of C++ libraries, provides
libraries that do what you are asking for, but it is somewhat complex
and spread out among multiple compoents. The result, however, is
relatively simple -- as demonstrated by the example at:
http://www.boost.org/libs/bind/bind.html#with_boost_function
See the doc of boost::bind and boost::function for more info.
http://www.boost.org/doc/html/function.html

Note that these utilities are likely to be integrated into the
next revision of the C++ standard, and are even already supported
by some compiler vendor(s) as documented in this technical report:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1540.pdf


Also of potential interest is the related boost signals library:
http://www.boost.org/doc/html/signals.html



hth,
Ivan
 
M

Michiel Salters

OvErboRed said:
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.

No. The MyClass part, which maps to the 'this' pointer in the method,
behaves like a hidden first argument. Therefore, you can't have a single
pointer type, just as there is no single function pointer type for both
'void foo(MyClass&)' and 'void bar(YourClass&)'.

Besides, how would you invoke it? If you have such a generic pointer,
and you initialize it with &MyClass::foo, and I invoke it on
std::string s;, what happens?

That said, you could use a union of funtion pointers.
e.g.
union Foo_or_Bar_MP {
void (Foo::*fooMethod)();
void (Bar::*barMethod)();
};
You must ensure that fooMethod is initialized when you
call pMyFoo->*(method.fooMethod). How you do that is up to you,
but it is legal and will work everywhere.

Regards,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top