473,372 Members | 866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 12176
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
5
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below...
3
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.