Private inheritance restricting base class interface

suggest change

Private inheritance is useful when it is required to restrict the public interface of the class:

class A {
public:
    int move();
    int turn();
};

class B : private A {
public:
    using A::turn; 
};

B b;
b.move();  // compile error
b.turn();  // OK

This approach efficiently prevents an access to the A public methods by casting to the A pointer or reference:

B b; 
A& a = static_cast<A&>(b); // compile error

In the case of public inheritance such casting will provide access to all the A public methods despite on alternative ways to prevent this in derived B, like hiding:

class B : public A {
private:
    int move();  
};

or private using:

class B : public A {
private:
    using A::move;  
};

then for both cases it is possible:

B b;
A& a = static_cast<A&>(b); // OK for public inheritance
a.move(); // OK

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Classes / structs:
* Private inheritance restricting base class interface

Table Of Contents
8 Arrays
11 Loops
19 Classes / structs
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype