returning derived class from overrided method, declared as returning base copy

Mel Viso Martinez

This is a recurrent problem once again. Someone know a easy way to do that? Imagine I have the following:

class Base
{
    public:
        ...
        Base property(const std::string& name)=0;
};
class Derived:public Base
{
    public:
        Derived();
        Derived(const Derived&& val);
        Base property(const std::string& name)
        {
            Derived z;
            return z;
        }
 }

There is a way for the Derived::property return being (internally) a Derived copy instead of only Base part copy, and with the Derived move constructor invoked?

May be a stupid question, but really I dont find solution. Why copy constructors on return dont copy the specialized class?

Thanks you!

TartanLlama

You can't do this.

Returning by value conceptually (ignoring RVO and move semantics) means making a copy of whatever you return by using the copy constructor of the type which the function is declared to return. If you return a Derived, a copy of type Base will be made and you'll lose the Derived part of the object. This is known as slicing.

If you want to return a Derived object as a Base, you'll need to use pointers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Returning a reference to the derived class from a base class method

Returning any derived class type from a method outside the base and derived class

Returning derived class as base generic class

Returning Derived templated class as Base class pointer

Overrided method calling from base class c++

Returning derived object pointer from virtual method

Returning a Task from a base method

Returning a derived type from factory class

Returning Class<T> From a Method

Error Assigning a Derived Class to the output of a function returning a base class

Calling overrided method on Derived class casted from void ptr causes segmentation fault

Clone derived class from base class method

Method returning from one class to another class

Returning a copy of class

Returning a subclass from its base class in swift

Returning multiple derived class instances from Elasticsearch using NEST and .NET

Return derived type from base class method

Returning a value from a block in class method

force base class to use its own method and not overrided method

Class method returning 0

How to copy data from base class to derived class fastly?

C++: call overrided method in base class's constructor

c# wont use an new overrided method in the base class

Method called in creator is called from base class but not from derived class

ModelMapper returning source object type when mapping derived class to a base class

copy & swap in base and derived class

× ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

C# interface method declared in Base class need not be again implemented in Derived class

How to use a virtual function from derived class, declared in another class with base class parameters?