Mail Archives: djgpp/1998/04/18/02:31:21
On Fri, 17 Apr 1998 tob AT world DOT std DOT com wrote:
> Also, HERO::action is of the wrong type to assign to member_func_ptr,
> (void (HERO::*) (void)). Making "action" a virtual function in "BASE"
> should do what you want.
Making it a (void (BASE::*)(void)) will ensure that you can take any
member function from any derived class that satisfies the void(void)
prototype. For the person who originally made the post, here is some
useful info:
- You are pointing to the RAW CLASS MEMBER FUNCTION. This is the
function that is not "tied" to any specific object. You must provide the
"this" pointer to the function by using the .* or ->* operators, ie:
BASE foo
HERO bar
// Take the pointer to the raw HERO::action function
foo.member_func_ptr = &HERO::action;
// Call foo.member_func_ptr as if it were a member of class HERO (which it
// just happens to actually be in this case!).
bar.*(foo.member_func_ptr)();
or
HERO * bar = new HERO;
bar->*(foo.member_func_ptr)();
(I'm pretty sure this is the proper syntax -- I can't find any actual
documentation on these operators, so I'm going from memory)
- You can call a member function of a related class (ie: parent class)
using this syntax. If class BASE had a function called base_action, you
could still do:
foo.member_func_ptr = &BASE::base_action;
bar.*(foo.member_func_ptr)();
- This is one of the least useful things you can do in C++. Anything you
can do with the .* operator, you can do much better with virtual
inheritance and special classes. It's a poorly documented feature and it
makes some code unreadable (except to yourself), so I recommend getting a
good book on inheritance and working through it before getting used to
this method.
/\/\att /\/\astracci mmastrac AT acs DOT ucalgary DOT ca
"Tout choses sont dites deja, mais comme personne n'ecoute, il faut
toujours recommencer."
- Raw text -