Connecting Tech Pros Worldwide Help | Site Map

Casting member function pointer

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:56 PM
Bren
Guest
 
Posts: n/a
Default Casting member function pointer


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! =-----

  #2  
Old July 19th, 2005, 05:56 PM
Bren
Guest
 
Posts: n/a
Default 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! =-----
  #3  
Old July 19th, 2005, 05:56 PM
Bren
Guest
 
Posts: n/a
Default 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! =-----
  #4  
Old July 19th, 2005, 05:57 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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 ?



  #5  
Old July 19th, 2005, 05:57 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.