Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing pure virtual functions from class template - parameter types

I want to create an abstract class template, forcing all instances to implement a doStuff function using a pure virtual function.

I have the following template:

template<class T>
class X
{
    public:
        X() {};
        virtual ~X() {};
        virtual X<T>& doStuff(X<T>& x) = 0;
};

And an instance with T= int:

class Y : public X<int>
{
    public:
        Y();
        virtual ~Y();
        Y& doStuff(Y& x) {
            Y res;
            Y& res2 = res;
            return res2;
        }
};

I get the error message:

In member function ‘Y& Y::doStuff(Y&)’: cannot declare variable ‘res’ to be of abstract type ‘Y’ because the following virtual functions are pure within ‘Y’: X<T>& X<T>::doStuff(X<T>&) [with T = int]

If I change the type of the argument to doStuff in Y, everything is fine:

class Y : public X<int>
 {
    public:
        Y();
        virtual ~Y();
        Y& doStuff(X<int>& x) {
            Y res;
            Y& res2 = res;
            return res2;
        }
};

Why can the parameter not be a reference to a Y object when Y implements X?

The return value of Y& does not create a similar error message.

Maybe I am using the wrong approach to achieve what I want - feel free to let me know.

like image 637
User Avatar asked Aug 08 '26 06:08

User


2 Answers

By setting Y& as the parameter you change the signature of doStuff and therefore res is abstract.

X<int>& is not Y& even though Y inherits X.

like image 113
Max Raskin Avatar answered Aug 09 '26 20:08

Max Raskin


Why can the parameter not be a reference to a Y object when Y implements X?

Because you have to give the exact signature as it was declared in the pure virtual function of the base.

That's why

class Y : public X<int> {
    // ...
    X<int>& doStuff(X<int>& x) override;
};

works.

See the working Live Demo.


Not to mention that returning a reference to a local variable is undefined behavior.

like image 22
πάντα ῥεῖ Avatar answered Aug 09 '26 19:08

πάντα ῥεῖ