Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Howto declare a function pointer to a method in a class? (http://www.velocityreviews.com/forums/t755094-howto-declare-a-function-pointer-to-a-method-in-a-class.html)

Francois Grieu 10-25-2011 01:36 PM

Howto declare a function pointer to a method in a class?
 
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.

Victor Bazarov 10-25-2011 01:41 PM

Re: Howto declare a function pointer to a method in a class?
 
On 10/25/2011 9:36 AM, Francois Grieu wrote:
> 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
--
I do not respond to top-posted replies, please don't ask

Francois Grieu 10-25-2011 01:54 PM

Re: Howto declare a function pointer to a method in a class?
 
On 25/10/2011 15:41, Victor Bazarov wrote:
> On 10/25/2011 9:36 AM, Francois Grieu wrote:
>> 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?


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

Christopher 10-25-2011 03:04 PM

Re: Howto declare a function pointer to a method in a class?
 
On Oct 25, 8:54*am, Francois Grieu <fgr...@gmail.com> wrote:
> On 25/10/2011 15:41, Victor Bazarov wrote:
>
> > On 10/25/2011 9:36 AM, Francois Grieu wrote:
> >> 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);

>


You can also check out boost::function and boost::bind

Victor Bazarov 10-25-2011 03:15 PM

Re: Howto declare a function pointer to a method in a class?
 
On 10/25/2011 9:54 AM, Francois Grieu wrote:
> On 25/10/2011 15:41, Victor Bazarov wrote:
>> On 10/25/2011 9:36 AM, Francois Grieu wrote:
>>> 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?

>
> 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.


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
--
I do not respond to top-posted replies, please don't ask

Krice 10-25-2011 04:29 PM

Re: Howto declare a function pointer to a method in a class?
 
On 25 loka, 16:36, Francois Grieu <fgr...@gmail.com> wrote:
> This works, but is ugly;


It's ugly, because it's not C++. Learn more about C++, should do
the trick.

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


I can see that.


All times are GMT. The time now is 03:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.