Re: Non-static member function as a callback
From: Arnaud Debaene (adebaene_at_club-internet.fr)
Date: 11/23/04
- Next message: Roy Fine: "Re: How good an encryption algorithm is this?"
- Previous message: mr.sir bossman: "Re: SetScrollSize"
- In reply to: Agoston Bejo: "Non-static member function as a callback"
- Next in thread: Agoston Bejo: "Re: Non-static member function as a callback"
- Reply: Agoston Bejo: "Re: Non-static member function as a callback"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 24 Nov 2004 00:18:31 +0100
Agoston Bejo wrote:
> Hi,
> I would like to do something like this:
>
>
> struct A {
> int f(float f);
> };
> ...
>
> int g(int(*f1)(float)) { return f1(6.5); }
>
> int main() {
> A a;
> g(a.*f); // or whatever. This won't compile, of course
> }
>
>
> Is this possible? If so, then how?
As steve as explianed, a member function pointer is not a simple function
pointer because :
- the member function takes an additrionnal, hidden, "this" parameter.
- the pointer itself may need to embed more information that just the
function address, in order to be able to call the right override in case of
virtual function. That's why the effective size of a member function pointer
can vary from 4 to 16 bytes, depending on the class hierarchy.
One possible solution to your problem is to replace your simple function
pointer in g by a template parameter, and then use boost::bind to create a
nullary functor calling your objec tinstance member function :
struct A
{
int f(float val);
};
template <typename FunctorType> int g(FunctorType functor)
{
return functor(6.5);
}
int main
{
A a;
g (boost::bind(&A::f, a));
}
See www.boost.org for details.
Arnaud
MVP - VC
- Next message: Roy Fine: "Re: How good an encryption algorithm is this?"
- Previous message: mr.sir bossman: "Re: SetScrollSize"
- In reply to: Agoston Bejo: "Non-static member function as a callback"
- Next in thread: Agoston Bejo: "Re: Non-static member function as a callback"
- Reply: Agoston Bejo: "Re: Non-static member function as a callback"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|