Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using subclass type parameters in virtual functions

I have this piece of code (contrived from my real-life trouble)

It cannot compile, complaining ExtendsB does not implement B::Run(A* a). However, it has no problems understanding the extension to A* Run();

class A { };

class ExtendsA : public A { };

class B
{
public:
    virtual ~B(){}  
    virtual void Run(A* a) = 0;
    virtual A* Run() = 0;
};

class ExtendsB : public B
{
public:
    virtual ~ExtendsB(){}

    // Not OK! It does not see it as an implementation of 
    // virtual void Run(A* a) = 0;
    virtual void Run(ExtendsA* ea) {}; 
    virtual ExtendsA* Run() { return new ExtendsA(); }; // OK
};

Why C++ allows to change the return type to a sub-class, but not the parameter type?

Is it a good rationale or just a missed point in the language specifications?

like image 595
Sam Avatar asked Sep 11 '12 11:09

Sam


People also ask

Can virtual functions have different parameters C++?

Master C and Embedded C Programming- Learn as you goYes, C++ virtual functions can have default parameters.

Can virtual functions have different return types?

The return type of an overriding virtual function may differ from the return type of the overridden virtual function. This overriding function would then be called a covariant virtual function. Suppose that B::f overrides the virtual function A::f .

Can virtual functions be friend of another class?

A virtual function can be a friend function of another class. Virtual functions should be accessed using pointer or reference of base class type to achieve runtime polymorphism. The prototype of virtual functions should be the same in the base as well as derived class.

Can virtual functions have default arguments?

Like any other function, a virtual function can have default arguments (§ 6.5. 1, p. 236). If a call uses a default argument, the value that is used is the one defined by the static type through which the function is called.


1 Answers

Why C++ allows to change the return type to a sub-class, but not the parameter type?

C++ standard allows you to use a Covariant return type while overidding virtual functions but does not allow you to modify the function parameters.And yes there is a good rationale behind it.

Rationale:

Overriding essentially means that either the Base class method or the Derived class method will be called at run-time depending on the actual object pointed by the pointer.
It implies that:
i.e: "Every instance where the Base class method can be called can be replaced by call to Derived class method without any change to calling code."

If the above rule was not in place it would leave a window to break the existing code by addition of new functionality(new derived classes).

If you have a function prototype in derived class which differs from base class virtual function w.r.t parameters then the function does not override the base class function, since the above rule gets broken.

However, Covariant return types do not break this rule, because upcasting happens implicitly and a Base class pointer can always point to a derived class object without any casting, hence the Standard enforces this condition of covariant return types on return types.

like image 86
Alok Save Avatar answered Sep 20 '22 16:09

Alok Save