0

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

Haranadh
  • 1,393
  • 4
  • 17
  • 34
  • 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 Answers4

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).

jergason
  • 19,478
  • 24
  • 93
  • 98
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
  • 238,196
  • 44
  • 399
  • 395
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
  • 104,737
  • 16
  • 128
  • 186
2

Make the constructor private.

  • Just making constructor private is not enough. refer Link provided by "Charles Salvia". Thanks to both of you. :) – Haranadh Apr 15 '10 at 14:55