Howto declare a function pointer to a method in a class?

F

Francois Grieu

Hello,

I'm trying to define a type suitable for a pointer to one
of several member functions of my class cBar (all functions
have the same interface, say accept an int and return void).


For now, I'm making a type tHandler suitable for a pointer
to a global function accepting an additional parameter "me",
holding a pointer to my class:


typedef void(*tHandler)(class cBar *const me, int val);

void Handler0(class cBar *const me, int val);
void Handler1(class cBar *const me, int val);

class cBar {
tHandler fCurrentHandler;
/*..*/
public:
inline void cBar::CurrentHandler(int val)
{
(*fCurrentHandler)(this,val);
}
inline cBar()
{
fCurrentHandler = Handler0;
CurrentHandler(0);
}
inline ~cBar()
{
CurrentHandler(-1);
}
};


This works, but is ugly; in particular Handler0/Handler1 should
be private methods of cBar, and tHandler a private type.

Any idea ?

TIA,
Francois Grieu


Note: this is not homework, I'm moving to C++ from an embedded C
background.
 
V

Victor Bazarov

I'm trying to define a type suitable for a pointer to one
of several member functions of my class cBar (all functions
have the same interface, say accept an int and return void).

Does this not work

typedef void (cBar::*tHandler)(int);

for some reason? What book on C++ are you reading that doesn't explain
pointers to member functions?

V
 
F

Francois Grieu

Does this not work

typedef void (cBar::*tHandler)(int);

for some reason?

Yes that's it!
What book on C++ are you reading that doesn't explain
pointers to member functions?

C++ For Mathematicians - An Introduction.. by Edward Scheinerman.

Many thanks.

Francois Grieu
 
V

Victor Bazarov

Yes that's it!


C++ For Mathematicians - An Introduction.. by Edward Scheinerman.

Get something that has more C++ in it. There are many decent books that
go into enough depth on C++ to be recommended by www.ACCU.org (see their
book section).

V
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top