Alf Steinbach: Here's an excerpt from
www.artimus.com which may help
you.
Nguyen Minh Huy
How to create final class in C++? Posted: Jun 18, 2005 6:02 AM
Re: How to create final class in C++? Posted: Jun 25, 2005 12:51 AM
Reply
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
Alf P. Steinbach wrote:
* Nataraj M: Hi, I just don't want anybody derive a class from my class. For
example:
///////////////////////
//MY CODE
class MyClass {
...
};
///////////////////////
/////////////////////
// Somebody's code
class HisClass : public MyClass { // This line should give a compile
time error
...
}
////////////////////
How can I make MyClass not to be a base class of other
The FAQ is at <url: http://www.parashift.c om/c++-faq-lite/>.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?