Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to "hide" a base class virtual function by making it pure virtual in derived classes?

Consider the example:

#include <iostream>

class A {
    public:
    virtual void f();
};

void A::f()
{
    std::cout << "f() from A\n";
}

class B: public A {
    public:
    virtual void f() = 0;
};

class C: public B {
    public:
    void f();
};

void C::f()
{
    std::cout << "f() from C\n";
}

int main()
{
    C o;
    o.f();
}

A::f() implementation is "hidden" from class C, which provides its own implementation for f() - effectively making A::f() more or less pointless. I see little value in such class hierarchy design, but my question whether this is a valid C++ or just "works" (such as undefined behaviours)?

like image 779
P.P Avatar asked Jan 23 '19 15:01

P.P


People also ask

Is it legal to have all member functions of a class to be pure virtual functions?

It is legal to have all member functions of a class be pure virtual functions. If the override specifier is added to the end of a member function declaration, what happens if the function is not specified as virtual in the parent class? There is a compiler error.

Can a pure virtual function be defined in the base class?

A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.

Can a virtual function declared as protected or private in the derived classes while it is declared as public in the base class )?

Can Virtual Functions be Private in C++? A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class.

Can a virtual function be reimplemented in a derived class?

Yes, Its correct that a Derived class has to OVERRIDE the function which is Pure Virtual in the Parent Class.


2 Answers

It is clearly allowed and supported by the standard (cf, for example, this online C++ standard draft), and thus clearly not undefined behaviour:

10.4 Abstract classes

5 [ Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. — end note ]

The effect is that your class B becomes abstract and any subclass - if it shall not be abstract, too - must define f() then; the implementation in class A can still be invoked through A::f(), such that it is - from the perspective of reusing the implementation - not pointless.

like image 140
Stephan Lechner Avatar answered Oct 18 '22 03:10

Stephan Lechner


This will safely achieve the goal of requiring the author of C to provide an implementation for f().

I would query why this is needed — if the base implementation is not "valid" in your design then why does it exist, and/or why is it virtual?

They can still invoke A::f(), anyway, so whether this can be deemed "hiding" is open to debate.

like image 3
Lightness Races in Orbit Avatar answered Oct 18 '22 04:10

Lightness Races in Orbit