mardi 2 février 2021

Do I have to override every function in base class, if I want to change the return type of a function?

I have an abstract class A that returns a pointer of its type. Then I have a derived class B which implements the methods defined in class A. I want the methods defined in class B to return class B pointers. Any idea how to do that?

class A {
    public:
        virtual A* foo() {}
}
class B: public A {}

B *x = new B();
x->foo();  // This should return B*

Do I need to override foo in B to return B*? Is there some better way ?

Aucun commentaire:

Enregistrer un commentaire