How can we define a funtion pointer to point a member function of a class?

G

Guest

Say we have the following code defining TMyMsgHandler and TMyClass

typedef void (*TOnMsgReceive) (TMyMessage Msg);
class TMyMsgHandler
{
public:
TMyMsgHandler();
virtual ~TMyMsgHandler();
TOnMsgReceive *OnMsgReceive;
};

class TMyClass1
{
public:
TMyClass();
~TMyClass();
void OnMsgReceive(TMyMessage Msg);
};
class TMyClass2
{
public:
TMyClass2();
~TMyClass2();
void OnMsgReceive(TMyMessage Msg);
};

....
TMyMsgHandler MyMsgHandler;
TMyClass1 MyClass1;
TMyClass2 MyClass2;

MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!

How can we define a funtion pointer to point a member function of a class?
 
J

John Harrison

Say we have the following code defining TMyMsgHandler and TMyClass

typedef void (*TOnMsgReceive) (TMyMessage Msg);
class TMyMsgHandler
{
public:
TMyMsgHandler();
virtual ~TMyMsgHandler();
TOnMsgReceive *OnMsgReceive;
};

class TMyClass1
{
public:
TMyClass();
~TMyClass();
void OnMsgReceive(TMyMessage Msg);
};
class TMyClass2
{
public:
TMyClass2();
~TMyClass2();
void OnMsgReceive(TMyMessage Msg);
};

...
TMyMsgHandler MyMsgHandler;
TMyClass1 MyClass1;
TMyClass2 MyClass2;

MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!

How can we define a funtion pointer to point a member function of a class?

You cannot.

You can do three things instead.

Either you can change you code so that it uses a member function pointer
instead of a function pointer, in your case that does not seem easy because
your two classes TMyClass1 and TMyClass2 are unrelated.

Or you can write a normal function that calls the member function that you
want to be called.

Or you can make the member functions static.

In the first two cases there is the drawback that you must have some way of
specifying which object you want to use to call the member function. Its
this requirement that newbies usually manage to forget, but in C++ any
non-static member function must be called on an object and its unreasonable
to expect C++ to magic an object out of thin air for you. The third method
gets round this problem by eliminating objects entirely. If that is your
preferred solution then I would stick with simple function pointers instead.

You could also read the FAQ

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

john
 
S

Siemel Naran

typedef void (*TOnMsgReceive) (TMyMessage Msg);
class TMyMsgHandler
{
public:
TMyMsgHandler();
virtual ~TMyMsgHandler();
TOnMsgReceive *OnMsgReceive;
};

class TMyClass1
{
public:
TMyClass();
~TMyClass();
void OnMsgReceive(TMyMessage Msg);
};
class TMyClass2
{
public:
TMyClass2();
~TMyClass2();
void OnMsgReceive(TMyMessage Msg);
};

...
TMyMsgHandler MyMsgHandler;
TMyClass1 MyClass1;
TMyClass2 MyClass2;

MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!

How can we define a funtion pointer to point a member function of a class?

To learn about member pointers, read the FAQ as others pointed out. You can
form a pointer that points to a member of a class, but to call the member
you need an object.

In the above scenario, you're asking for a pointer that points to a member
of an object, and C++ does not support this natively. You can build your
own though, and the basic idea is to write an abstract class with a pure
virtual operator(), derive from it a template class that holds a
pointer/reference to an object along with a pointer to a member function,
and operator() calls the member function on the stored object.
Alternatively, check out boost::function available at http://www.boost.org/.

If you're using Borland and don't care about writing ANSI compatible code,
then use their extension to support pointers to functions of objects. Then
your code should work:
MyMsgHandler.OnMsgReceive = MyClass.OnMsgReceive; <-- NOT VALID !!!

(Except MyClass is undefined, maybe you mean MyClass1?) So change
typedef void (*TOnMsgReceive) (TMyMessage Msg);

to

typedef __closure void (*TOnMsgReceive) (TMyMessage Msg);
 
S

Siemel Naran

Christopher Benson-Manica said:
Heaven forbid, although the __closure extension does come in handy.

Yes, indeed. I used to think that it would be nice if this were part of the
language, but one can always use boost::function instead. Most likely a
native implementation of __closure would be faster than boost::function
though.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top