Sobes.tech
Junior — Senior

Compilation error when applying protected inheritance

livecode

Task condition

Let's consider the following example: a base class B is declared with a virtual destructor, and a class Child inherits from it with the protected modifier. In the function checkChildB, an object of Child is created, but the pointer is declared as B*. It is necessary to understand why the code does not compile and what the problem is with using protected inheritance in this case.

class B {
public:
    virtual ~B() = default;
};

class Child : protected B {
};

void checkChildB() {
    B* b = new Child; // compilation error
}