Casting member function pointer

B

Bren

Hi all,

Given this situation:

class Base
{
typedef void (Base::*FN_FOO)();

virtual void Foo() = 0; // pure virtual
void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
}

class Derived : public Base
{
void Foo(); // implemented
void GetFoo(FN_FOO pfnFoo); // implemented
}

Then, somewhere in the code:

Derived derInstance;
FN_FOO pfnFoo;
derInstance.GetFoo(pfnFoo);

GetFoo is implemented like this:

void Derived::GetFoo(FN_FOO pfnFoo)
{
pfnFoo = &derInstance::Foo;
}

I get a compiler error that says cannot convert from Derived::* to
GraphicsEngine::*, conversion requires reinterpret_cast, C-style cast
or function-style cast.

Can anyone help me with the syntax for this cast?

Thanks!
 
B

Bren

I get a compiler error that says cannot convert from Derived::* to
GraphicsEngine::*, conversion requires reinterpret_cast, C-style cast
or function-style cast.

Duh, sorry, that SHOULD read:

I get a compiler error that says cannot convert from Derived::* to
Base::*, conversion requires reinterpret_cast, C-style cast or
function-style cast.

I was generalizing the actual names. :)

Thanks!
 
B

Bren

I get a compiler error that says cannot convert from Derived::* to
Base::*, conversion requires reinterpret_cast, C-style cast or
function-style cast.

Got it!

pfnFoo = = (void(Base::*)())&Derived::Foo
 
G

Gianni Mariani

Bren said:
Got it!

pfnFoo = = (void(Base::*)())&Derived::Foo

I'm not sure this does what you think it does.

It might work for you but it's certainly questionable if it's correct at
all.

What are you trying to do ?
 
R

Ron Natalie

Bren said:
void Derived::GetFoo(FN_FOO pfnFoo)
{
pfnFoo = &derInstance::Foo;
}

I get a compiler error that says cannot convert from Derived::* to
GraphicsEngine::*, conversion requires reinterpret_cast, C-style cast
or function-style cast.

Can anyone help me with the syntax for this cast?

You use a reinterpret cast, C-syle cast, or function cast.

pfnFoo = reinterpret_cast<FN_FOO>(&Derived::Foo);

You have a number of misconceptions and other problems as well.

1. You must form the pointer to member with the qulaified name (i.e.
Derived::Foo) rather than what you wrote which is ill-formed. You
also forgot the virtual key on the function GetFoo and a lot of semicolons.

2. There's no implicit Derived member pointer to Base member pointer.
Think of it this way: Every member of Base is a member of Derived
but not vice versa. The cast is potenitially unsafe.

3. I don't understand why you are screwing with the derived class member
at all. Since Foo is a virtual function in the base class, you could have
just written:
ptnFoo = &Base::Foo;
The virtual invocation gets applied after the member pointer is evaluated
so this will call Derived::Foo (for objects of type Derived) anyhow.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top