#ptrs to member functions
2 messages in this thread
John,
Sorry to bug you with my problems but I have no one here to turn to. I
have a class function, that needs to pass a member function's address to
another object's constructor. ie. class needs_function {
.. public:
needs_function(…,int (*func)(void *)); };
class has_function {
needs_function *nfp; public:
has_function();
int afunc(void *); };
has_function::has_function() {
nfp = new needs_function(…, afunc);
… } BS's book (outdated) says you have to trick it into accepting the
pointer, Zortech says essentially say the same — use typedef void
(PROC)(void *); None of these have worked. I've spent 2 or 3 hours trying
to make this work! Gave up, wrote friend functions and got on with life.
BUT I would really like this to work as I have the need to sub-class
'has_function' later on. Friend just doesn't seem to fit OOP, looks like a
hack. Thanks in advance. C h u c k
<<function needs a member function's address>>
The address of a member function is taken with a member pointer. See
January's CLM for a good coverage of the topic.
Notice thatinsead of a * in the pointer definition, the member-qualified
pointer is used instead. That is about all there is to it.
class needs_function {
public:
needs_function ( int (has_function::*func)(void*));
};
Zortech you say? They don't support member pointers, sorry. You will have
to trick it. Here is a portable, and not tricky, way:
int point_to_a (has_function& THIS, other_params)
{
return THIS.a (other_params);
}
Make a non-member shell function around each member function. Take the
address of the shell function instead.
–John