2
votes

Suppose I want to implement abstract behavior in a base class that is inherited by all derived classes. For example, a function that clones an object, applies some modification to it and returns the cloned and modified object (e.g. transposition, duplication, etc). The following code uses an even simpler function that merely returns the object itself:

class Base
{
public:
    virtual Base* yourself ()
    {
        return this;
    }
};

class Derived : public Base
{
public:
    ...
};

void main()
{
    Derived d;
    auto result = d.yourself();
}

If I didn't miss something, result will be of type Base*, where the intention clearly is to get an object of type Derived*.

Is there a way to have yourself() return the intended value for all derived classes without overriding it in all places (actually the whole point of inheritance is to implement each behavior only once)?

1
You could always perform an upcast if you desire.DeiDei
Yeah you could just use the addressof operator: auto result = &d;Justin

1 Answers

3
votes

This issue is fairly common, and you can get around it using templates. Something like this:

template <class T>
class Base
{
public:
    virtual T* yourself ()
    {
        return dynamic_cast<T*>(this);
    }
};

class Derived : public Base<Derived>
{
public:
    ...
};

It's called the Curiously recurring template pattern (CRTP)