Hi any one let me know How to make class not derivable at all. is there any way? please let me know. regards Hara
Asked
Active
Viewed 1,834 times
0
-
A similar question on SO was asked [here](http://stackoverflow.com/questions/1000908/is-it-possible-to-forbid-deriving-from-a-class-at-compile-time) – Charles Salvia Oct 24 '09 at 03:51
4 Answers
6
See this explanation on how do to it, and why it might not be a good idea, by Bjarne Stroustrup (creator of C++ himself).

Jamison Dance
- 19,896
- 25
- 97
- 99
4
If your class has a private constructor, there is no way for a derived class to be instantiated.
See "How can I set up my class so it won't be inherited from?" on the C++ FAQ Lite.

Mark Rushakoff
- 249,864
- 45
- 407
- 398
3
Make the ctor(s) private.
class not_derivable { private: not_derivable(){} };
class derived : public not_derivable {};
int main() { derived d; // diagnostic }
or the dtor:
class not_derivable { private: ~not_derivable(){} };
class derived : public not_derivable {};
int main() { not_derivable *nd = new not_derivable; derived d; //diagnostic }

dirkgently
- 108,024
- 16
- 131
- 187
2
Make the constructor private.

David Block
- 21
- 2
-
Just making constructor private is not enough. refer Link provided by "Charles Salvia". Thanks to both of you. :) – Hara Apr 15 '10 at 14:55