By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,542 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,542 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Kay wrote:


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


This is in the FAQ

http://www.parashift.com/c++-faq-lit...o-members.html
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a

<Kay> wrote in message
news:40***********************@news.optusnet.com.a u...
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-lit...o-members.html

john
Jul 22 '05 #3

P: n/a
THANK YOU...
This is in the FAQ

http://www.parashift.com/c++-faq-lit...o-members.html
--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #4

P: n/a
<Kay> wrote in message
news:40***********************@news.optusnet.com.a u...
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);

Jul 22 '05 #5

P: n/a
Siemel Naran <Si*********@remove.att.net> spoke thus:
If you're using Borland


Heaven forbid, although the __closure extension does come in handy.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #6

P: n/a
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
Siemel Naran <Si*********@remove.att.net> spoke thus:
If you're using Borland


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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.