471,585 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

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! =-----
Jul 19 '05 #1
4 12085
On Thu, 18 Sep 2003 11:26:18 -0400, Bren <ia***********@sympatico.ca>
wrote:
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!

-----= 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! =-----
Jul 19 '05 #2
On Thu, 18 Sep 2003 11:32:44 -0400, Bren <ia***********@sympatico.ca>
wrote:
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

-----= 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! =-----
Jul 19 '05 #3
Bren wrote:
On Thu, 18 Sep 2003 11:32:44 -0400, Bren <ia***********@sympatico.ca>
wrote:

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


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 ?

Jul 19 '05 #4

"Bren" <ia***********@sympatico.ca> wrote in message news:8q********************************@4ax.com...
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.
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ghostdog | last post: by
4 posts views Thread by Jacob Jensen | last post: by
5 posts views Thread by Ingo Nolden | last post: by
3 posts views Thread by joe bruin | last post: by
1 post views Thread by rahul8143 | last post: by
44 posts views Thread by Agoston Bejo | last post: by
8 posts views Thread by wkaras | last post: by
3 posts views Thread by Beta What | last post: by
4 posts views Thread by Wally Barnes | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.