Andrew
Andrew

Reputation: 3663

Inheritance type forcing

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:

Upvotes: 2

Views: 54

Answers (1)

Potatoswatter
Potatoswatter

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

Related Questions