473,698 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to prevent a class from being inheritable, in C++

Hi,

I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya
Jul 22 '05 #1
11 1611
Saumya wrote:
Hi,

I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya


Why do you want to do this? Explaining what you want to achieve with this
behaviour would help to find a meaningful solution.

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #2
As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second comment-only approach only
works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
isn't nice either. And in my opinion "final"izat ion imposes a large limitation to the usage of you class.

Therefore think twice, please, whether you can't change your design such not to need to make
your class behave like "final" any more.

Cheers,
Philipp.

"Saumya" <sa**********@y ahoo.com> wrote in message news:e9******** *************** ***@posting.goo gle.com...
Hi,

I didn't know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya

Jul 22 '05 #4
"Philipp Bachmann

As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second comment-only approach only
works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
isn't nice either. And in my opinion "final"izat ion imposes a large limitation to the usage of you class.


Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to a class. What
is the purpose of doing so?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
I think the only good reason is to force users of the class to aggregate
instead of inherit.
Consider a socket implementation, normally a socket class gives no meaning
to inherit from, it is like inheriting from std::iostream..
Sure there maybe where it could make sence, but for common use it doesn't..

Jesper

"Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message
news:40******** *******@gascad. at...
"Philipp Bachmann

As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as "final" in Java:
The first approach needs additional static member functions, the second comment-only approach only works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which isn't nice either. And in my opinion "final"izat ion imposes a large limitation to the usage of you class.
Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to

a class. What is the purpose of doing so?

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

Jul 22 '05 #6
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.

Also, it struck me that what if I was asked this question in some
technical interview ;-)
Jul 22 '05 #7
Saumya <sa**********@y ahoo.com> spoke thus:
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.
http://www.parashift.com/c++-faq-lit....html#faq-23.8
Also, it struck me that what if I was asked this question in some
technical interview ;-)


Just memorize the above URL? ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #8
I am really wondering who needs such a thing ...

Razvan

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> schrieb im
Newsbeitrag news:c1******** **@chessie.cirr .com...
Saumya <sa**********@y ahoo.com> spoke thus:
Not a very technical reason pal, but it just struck me while I was
learning Java and encountered the "final" keyword there.


http://www.parashift.com/c++-faq-lit....html#faq-23.8
Also, it struck me that what if I was asked this question in some
technical interview ;-)


Just memorize the above URL? ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

Jul 22 '05 #9
> Hmm. So it seems I am not alone.
All always wondered: Why would one want to impose such a 'finalization' to a class. What
is the purpose of doing so?


No you aren't alone. 'finalization' seems almost equivalent to asking:

"How can I prevent someone from reusing my code?"

The idiom seems to fit well in Java, however. I always do feel like
I'm wearing a straight-jacket when I code in Java. So heck, why not
'finalize' the classes to prevent there reuse. After all, you never
know when someone (possibly even yourself) might try to reuse it
without your permission possibly messing up the aesthetic symmetry
your 'pre-planned' class hierarchy. Shame on them.

"I have opinions, strong opinions, but, I don't always agree with
them."
-- G. W. Bush
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3193
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
2
2292
by: Mark | last post by:
Hi all, quick question, how do I make a define a class as being inheritable only?? Thanks Mark
20
2882
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
5
2138
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
3
1692
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that I can prevent the control from being designed by making it not the first class in the code file, but is there another more 'official' way to do this via attributes.
13
3157
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new class which derived from the String class, add the function there and then we would all use the new class which contained my function and all of the others etc....sounded relatively straight forward...
16
4240
by: Richard Brown | last post by:
Ok, now I am truely going nuts... probably why I didn't use the Class Builder in VB6 extensively. But, being the 'proper programmer' that I should, I'm trying to bite the bullet and build classes for each of my 'entities' that operate within the program, ie, passenger, vehicle, etc. Then seperate UI classes that link to these. (Partially, because eventually several add-ons will be able to reuse the code in the data classes). My...
5
3397
by: EqDev | last post by:
I have a class that is a control derived from UserControl. I want to use serialization and deserialization with this calss but I get an exception "Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.". I am not interested in serializing any member from the base class only the properties in the derived class. How can I prevent the entire base class from being...
5
3165
by: Jon Slaughter | last post by:
Why did microsoft seal these classes? I would like to add coordinate information to these classes but I can't derive from them ;/ It makes me wonder why microsft choose to prevent anyone from deriving from many classes. I can simply do class MyBitmap { public Bitmap B;
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.