473,485 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why make a class 'sealed'?

What would be the primary motivation to make a class 'sealed' (meaning that you
can't derive from it) in C++? (I understand that there is currently no sealed
keyword in C++, but that there are techniques to accomplish this. From what
I've heard, sealed may be added to the language in the future?)

I understand that there are compiler and efficiency reasons (optimizing away
virtual function calls, etc.) that justify it in other languages, but I'm not
interested in that. I'm asking strictly from an design standpoint (where minor
efficiency gains are not important).

I've briefly search Google/Google Groups, but didn't find anything that really
answered my question. Links, books, or other authoritive resources would be
appreciated, as well as personal (objective) responses.

Thanks -- Julie
Jul 22 '05 #1
6 2238
On Tue, 02 Mar 2004 16:57:04 -0800, Julie <ju***@aol.com> wrote:
What would be the primary motivation to make a class 'sealed' (meaning that you
can't derive from it) in C++? (I understand that there is currently no sealed
keyword in C++, but that there are techniques to accomplish this. From what
I've heard, sealed may be added to the language in the future?)

I understand that there are compiler and efficiency reasons (optimizing away
virtual function calls, etc.) that justify it in other languages, but I'm not
interested in that. I'm asking strictly from an design standpoint (where minor
efficiency gains are not important).

I've briefly search Google/Google Groups, but didn't find anything that really
answered my question. Links, books, or other authoritive resources would be
appreciated, as well as personal (objective) responses.

Thanks -- Julie


One reason I can think of off the top of my head: Prevent a user ("client")
of classes in an inheritance hierarchy--where "protected" access control is
employed--from deriving their own class for no purpose other than to gain
access to protected members of the base class. I've seen this used as an
argument against the wisdom of ever using "protected" at all as an access
control specifier for members.

Of course, C++'s access control mechanism really is meant to convey design
guidelines, not offer bullet-proof firewalling between classes by itself; a
client could simply edit a .h file to make everything public in the
hierarchy s/he wants access to, and get that access without having even to
create another class. To create truly robust separation typically costs you
in terms of code size and speed (for forwarding or proxy classes).

Those same issues would probably need to be considered if anything such as
"sealed" was to be up for consideration for addition to C++. Would it be a
symbolic thing, like "protected", or should it be given teeth?

Just my personal response (somewhere in the spectrum between objective and
subjective...)
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Julie wrote:
What would be the primary motivation to make a class 'sealed'... ?


Try comp.object, maybe?

-tom!
Jul 22 '05 #3

"Julie" <ju***@aol.com> wrote in message news:40***************@aol.com...
What would be the primary motivation to make a class 'sealed' (meaning that you can't derive from it) in C++? (I understand that there is currently no sealed
keyword in C++, but that there are techniques to accomplish this. From what
I've heard, sealed may be added to the language in the future?)

I understand that there are compiler and efficiency reasons (optimizing away
virtual function calls, etc.) that justify it in other languages, but I'm not
interested in that. I'm asking strictly from an design standpoint (where minor efficiency gains are not important).


Take the case of STL containers. It's usually advised not to derive from them.
Reason being that there is no virtual destructor. I have seen people still
derive from
STL containers due to various reasons.
IMO, if the class is designed to be used as it is, with no intentions of being
derived from
then probably "sealing" in such a case is fine. You ensure this way that users
of your class
do not inadvertently try deleting through base class pointers.

--
Sharad


Jul 22 '05 #4
"Sharad Kala" <no*****************@yahoo.com> wrote...

"Julie" <ju***@aol.com> wrote in message news:40***************@aol.com...
What would be the primary motivation to make a class 'sealed' (meaning that
you
can't derive from it) in C++? (I understand that there is currently no
sealed keyword in C++, but that there are techniques to accomplish this. From what I've heard, sealed may be added to the language in the future?)

I understand that there are compiler and efficiency reasons (optimizing away virtual function calls, etc.) that justify it in other languages, but I'm not interested in that. I'm asking strictly from an design standpoint

(where minor
efficiency gains are not important).
Take the case of STL containers. It's usually advised not to derive from

them. Reason being that there is no virtual destructor.
Absense of a virtual destructor is NOT a valid reason not to derive from
the class. The only limitation is that the derived object allocated from
freestore must not be deleted through a pointer to the base class. BFD!
I have seen people still
derive from
STL containers due to various reasons.
And I take it you never do it. Why? Just because, I suppose. Because
you read somewhere that "it's usually advised not to derive from them",
right?
IMO, if the class is designed to be used as it is, with no intentions of being derived from
then probably "sealing" in such a case is fine. You ensure this way that users of your class
do not inadvertently try deleting through base class pointers.


How do you ensure that users do not inadvertently write 'a[i]=i++' or use
reinterpret_cast when they shouldn't?

If there ought to be some mechanism for holding an idiot's hand, then why
should it only be in such narrow area as derived classes?

V
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:7me1c.167372$jk2.616040@attbi_s53...
"Sharad Kala" <no*****************@yahoo.com> wrote...

Absense of a virtual destructor is NOT a valid reason not to derive from
the class. The only limitation is that the derived object allocated from
freestore must not be deleted through a pointer to the base class. BFD!
True (except for BFD ;-))
I have seen people still
derive from
STL containers due to various reasons.


And I take it you never do it. Why? Just because, I suppose. Because
you read somewhere that "it's usually advised not to derive from them",
right?


Ofcourse not.
If your business logic requires that you do not want the whole vector interface
as it is or may be want to change some behavior then one has to inherit from it.
IMO, if the class is designed to be used as it is, with no intentions of

being
derived from
then probably "sealing" in such a case is fine. You ensure this way that

users
of your class
do not inadvertently try deleting through base class pointers.


How do you ensure that users do not inadvertently write 'a[i]=i++' or use
reinterpret_cast when they shouldn't?


Agreed that a user should know the lack of a sequence point or that the cast
is unsafe.
If there ought to be some mechanism for holding an idiot's hand, then why
should it only be in such narrow area as derived classes?


ok..so your point is that an ignorant user can do any kind of silly mistake and
where all you can stop him. OK, fine. I was saying that probably there would
be lesser mistakes if you didn't allow him to do so in the first place.

Best wishes,
Sharad

Jul 22 '05 #6
"Sharad Kala" <no*****************@yahoo.com> wrote
Take the case of STL containers. It's usually advised not to derive from them.
Reason being that there is no virtual destructor. I have seen people still
derive from
STL containers due to various reasons.
IMO, if the class is designed to be used as it is, with no intentions of being
derived from


Think of deriving from Standard Library containers as evolution in action. ;-)

Claudio Puviani
Jul 22 '05 #7

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

Similar topics

5
1975
by: Buddy Ackerman | last post by:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private SQLConnection class variable and open the connection it whenever it is instantiated...
5
14404
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
7
3634
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
8
1268
by: Keith Henderson | last post by:
I know I sound like a dolt for asking, but if I have a few functions that any objects in my code can call, mostly these are functions, how do I make them public to the application? I'm confused...
5
2127
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...
1
1100
by: ABC | last post by:
as subject. I have two classes, one is sealed class that will call or access the another class properties or methods. But, now, the another class is for future use and unknow what properties...
1
5156
by: Cody Powell | last post by:
I have a partial class split across two files. In the class declaration found in the first file, I state that it's a sealed class. In the class declaration found in the second file, I do not use...
8
2625
by: shawnz | last post by:
Is there any way to either derive a sealed class, or access private members of a sealed class? Either that, or is there a way to run internal methods in a sealed class outside the current assembly?...
3
2087
by: archana | last post by:
Hi all, I have created one sealed class having one constant. Can i access that constant of sealed class without creating object of that class. If yes will internally new object gets created....
0
7090
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,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7116
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,...
0
7161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
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...
0
4551
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...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
247
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...

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.