How to make class not derivable at all. is there any way?
Hi any one let me know How to 开发者_JAVA技巧make class not derivable at all. is there any way? please let me know. regards Hara
See this explanation on how do to it, and why it might not be a good idea, by Bjarne Stroustrup (creator of C++ himself).
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.
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 }
Make the constructor private.
精彩评论