I am working on a class (call it class D) that is derived from a class (call it class B) and will be inherited by another class (call it class E). I want instances of class E to have access to all the public functions of its parents but I specifically want to prevent class E from overriding any of the virtual functions in parent classes.
I thought by overriding the function in the private part of class D it would prevent the derived classes from overriding, but that appears not to be the case. This compiles and runs the function in class E:
#include <iostream>
//an abstract base class
class b
{
public:
virtual ~b() = default;
protected:
virtual void print() = 0;
virtual void print_two()
{
std::cout << "base class" << std::endl;
}
};
class d: public b
{
public:
virtual ~d() = default;
private:
void print() override // note this is not virtual -adding final here does the trick
{
std::cout << "class d" << std::endl;
}
using b::print_two; //this doesn't work either
};
class e : public d
{
public:
void print() override //why is this possible given the function it's overriding is private and non-virtual?
{
std::cout << "class e" << std::endl;
}
void print_two() override //why is this possible given the function it's overriding is private and non-virtual?
{
std::cout << "print two class e" << std::endl;
}
};
int main()
{
e foo;
foo.print();
foo.print_two();
}
So two questions out of this: Why can I override a function that is virtual in a grandparent but not virtual in a parent? Why can I override a function that is protected in a grandparent but private in a parent?
I tried it in g++ and clang and it compiles with no errors or warnings (-Wall -Wextra -Wpedantic)