Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:30:20+00:00 2026-06-01T12:30:20+00:00

This question is about overriding a virtual method in a derived class with a

  • 0

This question is about overriding a virtual method in a derived class with a different return type.
For the following code:

class father {
public:
  virtual father* ref() { return this; }
};

class child : public father {
  virtual child* ref() { return this; }
};

When I try to get a pointer directly, g++ (F15, g++4.5) reports “invalid conversion from father to child”

child m_child;
father* pf = &m_child;
child* pc = pf->ref();

I understand the ref() method in the child class is used and this is probably just a compile time type mismatch.

However, is there any way to do it without explicitly use a type cast?

Extra description:
I understand the reason why compiler report this error. What I need is something out of the box which can access the data in derived object without explicitly convert a pointer.

The father class is used to put different derived child objects into lists or vectors, so when an item is fetch from the list or vector, there is no way to tell which child class it is belong to.

I have internal method to record and check the child class type. But I dont want to explicitly convert the pointer.

For example, I would like to do something like this:

// assuming pf is pointer pointed to an item fetch from a vector
switch(fp->get_type()) {
case child_type1: fp->ref()->list1.push(data); break;
case child_type2: fp->ref()->list2.push(data); break;
case child_type3: fp->ref()->list3.push(data); break;
}

Right now, I need to explicitly declare a new variable or explicitly convert fp to the proper type in each case and each time I need to access a data in derived classes, which are tedious and confusing.

What I expect are: may be some boost libraries can do similar things in another way which I dont know yet, or may be the c++11 standard allows it but a special compiling parameter needs to be set?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:30:21+00:00Added an answer on June 1, 2026 at 12:30 pm

    Simple answer is “no”.

    You’ve lost the extra information (child* rather than father*) about ref when you threw away m_childs type information, by storing it as pointer to base (pf).

    One reason why it will never be possible without a cast is this example:

    class father {
    public:
      virtual father* ref() { return this; }
    };
    
    class childA : public father {
      virtual childA* ref() { return this; }
    };
    
    class childB : public father {
      virtual childB* ref() { return this; }
    };
    
    void should_never_compile(int i)
    {
       childA a;
       childB b;
       father pf;
       if( i ) { pf=&a; }
       else { pf=&b; }
    
       // This is evil and will not compile
       childA * pa = pf->ref();
    
       //But this is OK:
       childA * pa = dynamic_cast<childA*>(pf->ref() );
    }
    

    If you really want to make this doable without the dynamic cast you can just hide the dynamic cast (but it scares me a little)

    class father {
    public:
      virtual father* ref() { return this; }
      template<typename T> T* as() { return dynamic_cast<T*>(ref()); }
    };
    
    child m_child;
    father* pf = &m_child;
    child* pc = pf->as<child>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( This question about refactoring F# code got me one down vote, but also
I have this question about best practices in following examples: interface Request; interface Service
I have a question about overriding auto-generated accessor methods. The following would not work
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers
I have seen this question about deploying to WebSphere using the WAS ant tasks.
Follow up to this question about GNU make : I've got a directory, flac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.