I don't want to make the base class from being derived to a new class. Is there any way to achieve this? I want to tell the compiler that you can't inherit the base class just like a final keyword in java
Asked
Active
Viewed 317 times
-2
-
1In c++11 you can use `class Base final {` – RoQuOTriX Feb 17 '20 at 12:36
-
Not to be picky, but if a class can't be derived it can't be a base class, – anastaciu Feb 17 '20 at 12:39
-
Private contructor, like singletone design pattern – Ionut Alexandru Feb 17 '20 at 12:44
-
See this also https://www.interviewsansar.com/stop-a-class-to-be-inherited-cpp/ and this https://www.tutorialspoint.com/how-to-prevent-class-inheritance-in-cplusplus – I_Al-thamary Feb 17 '20 at 12:46
-
@IonutAlexandru one consequence of making the constructor private is that you cannot inherit from the class, but this does not mean that when you want a final class you should make the constructor private. A private constructor has other consequences – 463035818_is_not_an_ai Feb 17 '20 at 13:17
-
So guys, it seems declaring the class is final is the only option to prevent derived class from it – mohammed_thoyyib_tk Feb 19 '20 at 16:11
1 Answers
5
You can mark the class as final
to prevent derivation from it. E.g:
class A final { ... };

Maxim Egorushkin
- 131,725
- 17
- 180
- 271
-
1Just in case I would mention that `final` is available since c++ 11 – mvidelgauz Feb 17 '20 at 12:35