views:

161

answers:

5

Hi any one let me know How to make class not derivable at all. is there any way? please let me know. regards Hara

+5  A: 

See this explanation on how do to it, and why it might not be a good idea, by Bjarne Stroustrup (creator of C++ himself).

Jergason
nice. thanks a lot
Haranadh Gupta
+4  A: 

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
+2  A: 

A similar question on SO was asked here

Charles Salvia
+2  A: 

Make the constructor private.

David Block
Just making constructor private is not enough. refer Link provided by "Charles Salvia". Thanks to both of you. :)
Haranadh Gupta
+3  A: 

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