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