Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forbid C++ derived class to derive from base, but allow from another derived class

Tags:

People also ask

How can you prevent all the member methods of base class from being overridden in C++?

In C++, there's no way to forbid it, it's just that by definition of "override", only virtual functions can be "overridden".

Can the private members of base class be used in the derived class?

Private members of the base class cannot be used by the derived class unless friend declarations within the base class explicitly grant access to them.

Can a base class be derived from another class?

When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and finalizers. The derived class reuses the code in the base class without having to reimplement it. You can add more members in the derived class.

Can a derived class inherit from two base classes?

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.


Given this scenario:

class GrandParent {};
class Parent : public GrandParent {};
class Child : public Parent {}; /// Ok
class Child : public GrandParent {}; /// Is it possible to force a compilation error?