|
Re: How to create final class in C++?
|
Posted: Jun 24, 2005 9:51 PM
|
|
responding to Nguyen
In C++ there is no keyword (final) to declare a class as non-inheritable as in Java. But then C++ has its own features which you may exploit to get the same behaviour.
Bsically it uses concepts of private constructor and friend class.
Idea is if you derive(virtual public) a class from another class having private constructors, you cannot create an object of derived class.
To avoid this you need to declare derived (Final class) class as friend of base class. So that now if some one tries to inherit from this Final class, compilation gives error as this class cannot call constructor of its super class i.e. Final class's super class i.e. base class that has private constructor.
Hope this helps.
regards, Shashank
|
|