Re: Passing a member function to another func.
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Sun, 14 May 2006 06:59:21 -0700
dududuil wrote:
Hi,
I am trying to pass one class member (NOT STATIC!) to another func,
and fail to compile.
I try to do something like this:
struct A { void fa1(); void fa2(); };
struct B {
void (*m_fb)();
void SetFA(void (*fa)()) {m_fb = fa;};
};
void main()
{
A a;
B b;
b.SetFA( &a.fa1 ); // Compile error: C2276!!!
}
Note: func A::fa can not be static.
How can I do this?
What I want is to decide on run time, which method to call. I can
simply use an "if" statement, but I worry about performance.
David gave you the answer - you need to use a "pointer to member function"
instead of an ordinary function pointer.
If you're worried about performance, the if statement will likely outperform
the pointer to member, unless you have a large number of alternatives. If
it's really just two, then just use the if statement.
-cd
.
- Follow-Ups:
- Re: Passing a member function to another func.
- From: Abdo Haji-Ali
- Re: Passing a member function to another func.
- Prev by Date: Re: Polymorphism?
- Next by Date: Base64Encode/Decode and Unicode
- Previous by thread: Re: Passing a member function to another func.
- Next by thread: Re: Passing a member function to another func.
- Index(es):
Relevant Pages
|