Reputation: 3663
Is it possible to make inheritance forced to be public, private or protected?
Ie:
class block: public shape{ // Only way to inherit from shape
};
// class block: private shape{}; // throws an error
To make things more clear:
shape
be public. eg. If a class tries to privately inherit from shape
that should trigger a compile error.Upvotes: 2
Views: 54
Reputation: 137800
There is generally no way that a base can dictate the interfaces of its derived classes. Such constraint doesn't seem to solve any problem, and "forced publicity" could easily be worked around by restricting access to the derived class.
For example, Square
here provides a public interface to Shape
, but nobody can access Square
anyway due to access protection.
class SquaresAreAllMine {
private:
class Square : public Shape {};
};
(Declaring signatures of virtual functions is an exception, as there is no way for a derived class to revert to non-virtual status. This is not really intended to be restrictive, though.)
Upvotes: 4