By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Don't want to make my class as base class

P: n/a
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

Jul 29 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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/>.

--
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?
Jul 29 '05 #2

P: n/a
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.com/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?


Jul 29 '05 #3

P: n/a
Frank Chang wrote:

Alf Steinbach: Here's an excerpt from www.artimus.com which may help
you.

I don't think that Alf needs that help.
Bsically it uses concepts of private constructor and friend class.


And this differs exactly in which way from what is described in the FAQ?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 29 '05 #4

P: n/a
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.

Karl Heinz Buchegger wrote:
Frank Chang wrote:

Alf Steinbach: Here's an excerpt from www.artimus.com which may help
you.


I don't think that Alf needs that help.
Bsically it uses concepts of private constructor and friend class.


And this differs exactly in which way from what is described in the FAQ?

--
Karl Heinz Buchegger
kb******@gascad.at


Jul 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.