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 8544439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:33:30+00:00 2026-06-11T12:33:30+00:00

I have a class hierarchy with multiple levels of inheritance. cloneable declares a pure

  • 0

I have a class hierarchy with multiple levels of inheritance.

  1. cloneable declares a pure virtual member function returning cloneable *.
  2. base derives from cloneable, but does not declare any member functions.
  3. Finally, derived derives from base and defines the virtual function, but overrides the return type to derived *.

Calling the virtual function via a base pointer to derived object returns cloneable *. I was expecting base * because the implementation of the virtual function returns derived * which is convertible to base *. What is going on here?

If I declare the pure virtual function in base, I can finally get base * from it, but I do not understand why this declaration is necessary.

Code:

struct cloneable
{
  virtual cloneable * clone() = 0;
};

struct base : cloneable 
{
// virtual base * clone() = 0;    // this line resolves the compile error
};

struct derived : base
{
  virtual derived * clone () { return new derived; }
};

int main(int, char**)
{
  derived d;
  base * bp = &d;
  base * bbp = bp->clone();  // error: invalid conversion 
                             // from ‘cloneable*’ to ‘base*’      
  return 0;  
}

Note: I’ve deliberately omitted the virtual destructor to shorten the code example.

  • 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-11T12:33:31+00:00Added an answer on June 11, 2026 at 12:33 pm

    Exactly how do you think the compiler should guess that you want a version returning a base*, without any declaration?


    While the above question answers your direct question, I feel that I should also add some advice.

    First of all,

    • do make the clone function const, so that it can be called on a const object or via an rvalue expression.

    I.e.,

    virtual cloneable* clone() const;
    

    Secondly, to create a clone of an object,

    • return new T( *this ) (using copy constructor), not new T (using default constructor).

    And third,

    • for safety, for the publicly available clone operation return a smart pointer such as a unique_ptr<MyClass>, not a raw pointer.

    However, with the change of return type to smart pointer, you will no longer benefit directly from the C++ support for covariant function results, which is only for raw pointers and references. So one way to do that is to have a non-public raw pointer result implementation, which can have covariant result type, and simply a typed public wrapper that returns a smart pointer. In effect you’re then implementing the covariance for the public interface, yourself, and it can look like this:

    #include <memory>       // std::unique_ptr
    using namespace std;
    
    class Base
    {
    private:
        virtual Base* virtualClone() const
        {
            return new Base( *this );
        }
    
    public:
        unique_ptr< Base > clone() const
        {
            return unique_ptr< Base >( virtualClone() );
        }
    };
    
    class Derived
        : public Base
    {
    private:
        virtual Derived* virtualClone() const
        {
            return new Derived( *this );
        }
    
    public:
        unique_ptr< Derived > clone() const
        {
            return unique_ptr< Derived >( virtualClone() );
        }
    };
    
    int main()
    {
      Derived d;
      Base* bp = &d;
      unique_ptr< Base > bbp = bp->clone();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class hierarchy and i am writing a virtual function in it.
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
Suppose you have a Java class hierarchy of about 30 classes, with a base
Let's say I have the following class hierarchy in C++: class Base; class Derived1
I have C++ class with multiple parents; each parent defines a function with a
I have a hierarchy that looks something like this: class Base { public: void
I have a class hierarchy in PHP 5.2.9 . The base class has a
I sometimes end up with a class hierarchy where I have an abstract base
Consider the following hierarchy: class Base { virtual void Method() = 0; virtual void
I have a class hierarchy as follows and binding to the VisibleRange property is

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.