Skip to main content
gamedev.net
🔒 Locked

Function pointers inside structs?

Started by gandolf1212 1 reply 600+ views
Original Post
gandolf1212
gandolf1212
Anyone know how you would do this?

struct Creature{
   char Name[10];

   Creature();
   void (*Rename)();
   void NameBob();
};

Creature Gabu;

Creature::Creature(){
   strcpy(Name, "Gabu");
   Gabu.Rename = &NameBob
   o<rint(){
   o<.

Btw, if you can''t figure out what it''s supposed to do (lol?) Rename should point to name bob to change the name to Bob.
Thanks alot for any help (or even unhelpful things that where meant to help =) ) Gandolf
I think there's to much blood in my caffeine system.
dacrisxp
dacrisxp
Huh?

Shouldn''t you be calling it as
(*Gabu.Rename()); not Gabu.Rename(); ?
invective
invective
I would use a typedef to define your function pointers, since its easier to read and you can call your functions like myfunction () instead of (*myfunction)()


typdef void ( * function_ptr ) ( void );
function_ptr myfunction = somefunction;
myfunction ();

But to answer your question, pointers to NON STATIC MEMBER functions (class or structs) have an implicit this pointer as their first parameter (so they know what object invoked them). Because of this, you must qualify the function name with the class name.

typedef returnType (ClassName::*memberPointerName)( parameters);


For more info, see here:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.