Don't want to make my class as base class

N

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

Thanks,
Nataraj
 
A

Alf P. Steinbach

* 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.com/c++-faq-lite/>.
 
F

Frank Chang

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
 
F

Frank Chang

Karl, This post was intended for Nataraj M not Alf Steinbach. If one
goes to the link you are referring to, then one has to do a search to
find this topic. I thought I would save Nataraj M the time to do this
search. Anyway, I think we are quibbling about something that has
nothing to do with C++. Thank you for your moderator comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top