Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting member function pointer

Bren
Guest
 
Posts: n/a
#1: Jul 19 '05

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!




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Bren
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Casting member function pointer


On Thu, 18 Sep 2003 11:26:18 -0400, Bren <iambrenNOSPAM@sympatico.ca>
wrote:
[color=blue]
>I get a compiler error that says cannot convert from Derived::* to
>GraphicsEngine::*, conversion requires reinterpret_cast, C-style cast
>or function-style cast.[/color]

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!



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Bren
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Casting member function pointer


On Thu, 18 Sep 2003 11:32:44 -0400, Bren <iambrenNOSPAM@sympatico.ca>
wrote:
[color=blue]
>I get a compiler error that says cannot convert from Derived::* to
>Base::*, conversion requires reinterpret_cast, C-style cast or
>function-style cast.[/color]

Got it!

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



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Gianni Mariani
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Casting member function pointer


Bren wrote:[color=blue]
> On Thu, 18 Sep 2003 11:32:44 -0400, Bren <iambrenNOSPAM@sympatico.ca>
> wrote:
>
>[color=green]
>>I get a compiler error that says cannot convert from Derived::* to
>>Base::*, conversion requires reinterpret_cast, C-style cast or
>>function-style cast.[/color]
>
>
> Got it!
>
> pfnFoo = = (void(Base::*)())&Derived::Foo
>[/color]

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 ?



Ron Natalie
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Casting member function pointer



"Bren" <iambrenNOSPAM@sympatico.ca> wrote in message news:8qijmvc1mte46ilr37v0pjgrfq1icr0fk8@4ax.com...
[color=blue]
> 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?[/color]

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.


Closed Thread