• Advertisement

Archived

This topic is now archived and is closed to further replies.

FunctionPointer to a class-member

This topic is 5753 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to make a pointer to a function in a class, or call it in any general way? I wanna make a function pointer be able to point to different functions in different classes.
  
FunctionPointer = _Class1::Std_KeyDown();
FunctionPointer = _Class2::Std_KeyDown();
  
The compiler says: cannot convert from ''void (__thiscall _Class1::*)(unsigned char)'' to ''void (__cdecl *)(unsigned char) Thanks! /Fredrik Olsson

Share this post


Link to post
Share on other sites
Advertisement
The OO alternative is virtual functions, and the generic alternative is functors, but yes you can make pointers to members check the link above or search the forum.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ ]

Shamelessly ripped from Oluseyi

Share this post


Link to post
Share on other sites
Now, I think I understand how memberfunction-pointers works, and I can''t find a good solution to my problem:


  
class object
{
void *boundObject;
int objecttype;

void mousemove(int x, int y);
void (*fp_mousemove) (int x, int y);
}

// These are the different objects that

// should be bindable to "class object"

class button
{
void BindToObject(object *o);
void mousemove(int x, int y);
}

class image
{
void BindToObject(object *o);
void mousemove(int x, int y);
}


I wanna be able to call object->mousemove() and have it call the
mousemove-function specified by the bound object (botton or image).
However, it seems as if this can''t be done, since button/image::mousemove is (and cant be) const.

The only solution I can come up with woul''d be to make a switch-statement that would check the objecttyp and call the appropriate function. But this would be a pretty ugly approach, when using lots of objects.

The way I wanted it to work was that image::BindToObject(object) would set the object::fp_mousemove to image::mousemove.

Sorry if I explained it bad..

Share this post


Link to post
Share on other sites
Why not make a baseclass from where image and button are derived image and button and derived. Declare virtual functions and implement them in image and button. This way "object" will only have to know the base class and all calls to the virtual functions go to the correct derived objects:

class bindBase {
virtual int Type();
virtual void BindToObject(object *o);
virtual void mousemove(int x, int y);
};

class object {
bindBase *boundObject;
void mousemove(int x, int y) {
if(boundObject) boundObject->mousemove(x,y);
else { ... };
}
};

class button : public bindBase {
int Type();
void BindToObject(object *o);
void mousemove(int x, int y);
}
class image : public bindBase{
int Type();
void BindToObject(object *o);
void mousemove(int x, int y);
}


[edited by - nano on April 20, 2002 12:18:19 PM]

Share this post


Link to post
Share on other sites
On second thought, that doesn't provide a general pointer to the object...

I wanna be able to call Object->mousemove(x,y) and have it call the "attached" class-function (ie. button, image).

The only solution I can come up with is to make a switch-case statement in each of "class object"'s functions.

Not a smooth way of solving it...

[edited by - FXO on April 22, 2002 9:02:41 AM]

Share this post


Link to post
Share on other sites
GameDev has some excellent articles on a game windowing system (the first of which is found here. They''re a couple years old, but they''re rather good.

Not directly related to what you''re asking or attempting, but you might want to correlate your findings here with your own research.

Good luck!

Share this post


Link to post
Share on other sites

  • Advertisement