Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing member function pointer

3 views
Skip to first unread message

David Orr

unread,
Jul 25, 1998, 3:00:00 AM7/25/98
to
How can I pass a class member function as a parameter to another
function?
Consider:

typedef void (*CALLBACKFUNC) (char*);

class ClassA
{
//...//
CALLBACKFUNC CallBackFunc;
void RunCallBackFunc( char* y );
public:
void SetCallBackFunc ( CALLBACKFUNC x );
};

void ClassA::SetCallBackFunc( CALLBACKFUNC x){ CallBackFunc = x; }
void ClassA::RunCallBackFunc( char* y ){ CallBackFunc( y ); }


Class ClassB
{
//...///
public:
void DoCallBackStuff( char* z ); <-- uses members and functions
of ClassB
};

--------------------------------
ClassA ClassWantingCallBack;
ClassB ClassWithCallBack;

ClassWantingCallBack.SetCallBackFunc( DoCallBackStuff );

Thank you in advance for your input.

David

--
------------------------------------
IBERT
PO Box 4753
Glendale, CA 91222
800/347-2251

http://ibert.org

Siemel Naran

unread,
Jul 25, 1998, 3:00:00 AM7/25/98
to
>How can I pass a class member function as a parameter to another
>function?

>typedef void (*CALLBACKFUNC) (char*);

If the function is a member of class ClassB, then use this
typedef void (ClassB::*CALLBACKFUNC) (char*);

To avoid cluttering the namespace, consider putting this typedef in
the definition of classA, in the public interface, of course.

Remember to prototype the existence of classB.


>void ClassA::RunCallBackFunc( char* y ){ CallBackFunc( y ); }

Member functions must be called with either of these two : .* ->*
It's good practice to call non-member functions with a star: *

To call a member function, you need to supply the object the function
must work on.

Figure the concrete syntax for yourself -- an exercise!

--
----------------------------------
Siemel B. Naran (sbn...@uiuc.edu)
----------------------------------

Sriraman.R

unread,
Aug 3, 1998, 3:00:00 AM8/3/98
to
Hi ,
See comments inserted...
David Orr wrote:

> Class ClassB
> {
> //...///
> public:
> void DoCallBackStuff( char* z ); <-- uses members and functions
> of ClassB

//define the above function as below:
static void DoCallBackStuff( char* z ){cout << z << endl;}

> };
--
With regards
Sriraman.R
------------------------------------------
Nothing is impossible in this world
Even the word impossible says I'm possible.
------------------------------------------

Sriraman.R

unread,
Aug 3, 1998, 3:00:00 AM8/3/98
to Sriraman.R
Hi David ,
Sorry i left one point
In the SetCallback function give the following as the parameter.
ClassWantingCallBack.SetCallBackFunc( ClassWithCallBack.DoCallBackStuff
);

Thanks,
--
With regards
Sriram
HCL-CISCO ODC

------------------------------------------
Nothing is impossible in this world
Even the word impossible says I'm possible.
------------------------------------------

0 new messages