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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:42:05+00:00 2026-05-24T07:42:05+00:00

In C++ we can do this: struct Base { virtual Base* Clone() const {

  • 0

In C++ we can do this:

struct Base
{
   virtual Base* Clone() const { ... }
   virtual ~Base(){}
};

struct Derived : Base
{
   virtual Derived* Clone() const {...} //overrides Base::Clone
};

However, the following won’t do the same trick:

struct Base
{
   virtual shared_ptr<Base> Clone() const { ... }
   virtual ~Base(){}
};

struct Derived : Base
{
   virtual shared_ptr<Derived> Clone() const {...} //hides Base::Clone
};

In this example Derived::Clone hides Base::Clone rather than overrides it, because the standard says that the return type of an overriding member may change only from reference(or pointer) to base to reference (or pointer) to derived. Is there any clever workaround for this? Of course one could argue that the Clone function should return a plain pointer anyway, but let’s forget it for now – this is just an illustratory example. I am looking for a way to enable changing the return type of a virtual function from a smart pointer to Base to a smart pointer to Derived.

Thanks in advance!

Update: My second example indeed doesn’t compile, thanks to Iammilind

  • 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-05-24T07:42:06+00:00Added an answer on May 24, 2026 at 7:42 am

    You can’t do it directly, but there are a couple of ways to simulate it, with the help of the Non-Virtual Interface idiom.

    Use covariance on raw pointers, and then wrap them

    struct Base
    {
    private:
       virtual Base* doClone() const { ... }
    
    public:
       shared_ptr<Base> Clone() const { return shared_ptr<Base>(doClone()); }
    
       virtual ~Base(){}
    };
    
    struct Derived : Base
    {
    private:
       virtual Derived* doClone() const { ... }
    
    public:
       shared_ptr<Derived> Clone() const { return shared_ptr<Derived>(doClone()); }
    };
    

    This only works if you actually have a raw pointer to start off with.

    Simulate covariance by casting

    struct Base
    {
    private:
       virtual shared_ptr<Base> doClone() const { ... }
    
    public:
       shared_ptr<Base> Clone() const { return doClone(); }
    
       virtual ~Base(){}
    };
    
    struct Derived : Base
    {
    private:
       virtual shared_ptr<Base> doClone() const { ... }
    
    public:
       shared_ptr<Derived> Clone() const
          { return static_pointer_cast<Derived>(doClone()); }
    };
    

    Here you must make sure that all overrides of Derived::doClone do actually return pointers to Derived or a class derived from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why can't I do this: struct sName { vector<int> x; }; It takes only
I can do this in c++/g++: struct vec3 { union { struct { float
Can anybody explain me this statement! pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
I have this: struct.pack('I', 0b10101010101100101010001000001000).encode('base64') which is good for converting 32 bits to base
class data is like this: struct Base_data { public: Base_data(){ protocolname = Base; }
Can this be done by setting a property? I'd prefer that approach then to
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
Can this be done? It seems like this should be possible. In general, I
Can this be done in JavaScript? type == 1 ? function1() : function2();
Can this be simplified to a one liner? Feel free to completely rewrite it

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.