473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

std destructors

why don't the std classes have virtual destructors?

doesn't it make sense to want to inherit from these classes and
add/change functionality?

Jun 21 '06 #1
11 1376

jq********@gmail.com wrote:
why don't the std classes have virtual destructors?

doesn't it make sense to want to inherit from these classes and
add/change functionality?


It is usually not a good idea to derive from concrete classes because
of the partial assignment problem and mixed-type assignment problem. It
also shows the need for new potential abstractions in your design.

Check out item 33 on Scott Meyers' More Effective C++. He explains it
all clearly. (That's a book that we should all keep handy).

"Object Oriented Design Heuristics" also mentions the problem and its
implications.

Belebele

Jun 21 '06 #2
I guess my question is... why are the std classes concrete classes?
Belebele wrote:
jq********@gmail.com wrote:
why don't the std classes have virtual destructors?

doesn't it make sense to want to inherit from these classes and
add/change functionality?


It is usually not a good idea to derive from concrete classes because
of the partial assignment problem and mixed-type assignment problem. It
also shows the need for new potential abstractions in your design.

Check out item 33 on Scott Meyers' More Effective C++. He explains it
all clearly. (That's a book that we should all keep handy).

"Object Oriented Design Heuristics" also mentions the problem and its
implications.

Belebele


Jun 21 '06 #3
jq********@gmail.com wrote:
why don't the std classes have virtual destructors?

doesn't it make sense to want to inherit from these classes and
add/change functionality?


std::exception has a virtual destructor, to name just one of many. If
your concern is the coding style guideline that base classes should have
virtual destructors, that guideline is simplistic. A virtual destructor
is needed only if the design of the class calls for deleting objects of
derived types through pointers to the base type. For beginners that may
be too complicated, hence the broader guideline.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 21 '06 #4
Thanks. This resolves an argument with a co-worker (he was under the
false impression that the derived class' constructor wouldn't be called
even if the pointer using to delete it was of the derived class).
Pete Becker wrote:
jq********@gmail.com wrote:
why don't the std classes have virtual destructors?

doesn't it make sense to want to inherit from these classes and
add/change functionality?


std::exception has a virtual destructor, to name just one of many. If
your concern is the coding style guideline that base classes should have
virtual destructors, that guideline is simplistic. A virtual destructor
is needed only if the design of the class calls for deleting objects of
derived types through pointers to the base type. For beginners that may
be too complicated, hence the broader guideline.

--

Pete Becker
Roundhouse Consulting, Ltd.


Jun 21 '06 #5
Pete Becker wrote:
A virtual destructor
is needed only if the design of the class calls for deleting objects of
derived types through pointers to the base type.


That is always the case when the derivation is public. Public
inheritance is an idiom that states that the derived class object can
be used as a base class object at all times (Liskov Substitution
Principle).

The implementor of the derived class cannot prevent clients from
keeping references to objects of the publicly derived class through
pointers of the base class (and deleting them anytime). It is then a
good practice to make the base class destructor virtual to make it
harder to use that base class incorrectly.

Private and protected inheritance is another matter, as the
substitution principle does not apply.

Jun 21 '06 #6
Belebele wrote:
Pete Becker wrote:
A virtual destructor
is needed only if the design of the class calls for deleting objects
of derived types through pointers to the base type.
That is always the case when the derivation is public.


Never say "always".
Public
inheritance is an idiom that states that the derived class object can
be used as a base class object at all times (Liskov Substitution
Principle).

The implementor of the derived class cannot prevent clients from
keeping references to objects of the publicly derived class through
pointers of the base class (and deleting them anytime). It is then a
good practice to make the base class destructor virtual to make it
harder to use that base class incorrectly.

Private and protected inheritance is another matter, as the
substitution principle does not apply.


You're making too many assumptions. Protected inheritance could also
mean substitution, only the region is limited to the members of the
class itself or the derived class[es].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #7
Belebele wrote:
Pete Becker wrote:
A virtual destructor
is needed only if the design of the class calls for deleting objects of
derived types through pointers to the base type.

That is always the case when the derivation is public.


No, it's only the case when your design calls for deleting derived
objects through pointers to the base.
Public
inheritance is an idiom that states that the derived class object can
be used as a base class object at all times (Liskov Substitution
Principle).
It doesn't follow that you need to delete derived objects through
pointers to the base. Further, that's not the only use for public
inheritance.

The implementor of the derived class cannot prevent clients from
keeping references to objects of the publicly derived class through
pointers of the base class (and deleting them anytime). It is then a
good practice to make the base class destructor virtual to make it
harder to use that base class incorrectly.


If the design of the base class doesn't call for deleting derived
objects through pointers to the base, then making the destructor virtual
doesn't make it harder to use the class incorrectly. It just removes one
of the symptoms.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 22 '06 #8

Victor Bazarov wrote:
Belebele wrote:
That is always the case when the derivation is public.
Never say "always".


That's true. You can frequently find bizarre uses of the public
inheritance idiom. When those pop up in my head, I try hard to sit back
and wait for them to go away, with mixed results.
You're making too many assumptions. Protected inheritance could also
mean substitution, only the region is limited to the members of the
class itself or the derived class[es].


I would classify such uses as bizarre, if anything, due to my lack of
understanding.

Jun 22 '06 #9
jq********@gmail.com wrote:
Thanks. This resolves an argument with a co-worker (he was under the
false impression that the derived class' constructor wouldn't be
called even if the pointer using to delete it was of the derived
class).


Please don't top-post. See item 4 of the FAQ entry below:

<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>

Brian

Jun 22 '06 #10
On 21 Jun 2006 16:37:25 -0700, jq********@gmail.com wrote:
Belebele wrote:
jq********@gmail.com wrote:
> why don't the std classes have virtual destructors?
> doesn't it make sense to want to inherit from these classes and
> add/change functionality?


It is usually not a good idea to derive from concrete classes because
of the partial assignment problem and mixed-type assignment problem. It
also shows the need for new potential abstractions in your design.

Check out item 33 on Scott Meyers' More Effective C++. He explains it
all clearly. (That's a book that we should all keep handy).

"Object Oriented Design Heuristics" also mentions the problem and its
implications.


I guess my question is... why are the std classes concrete classes?


Some are. The C++ Standard library consists of 3 larger libraries
which have been introduced successively in the last 20 or so years.
Each of those libraries follows a different 'paradigm':
1. The C-Standard part is a procedural library
2. iostreams are designed according to the OO paradigm
3. the standardized subset of STL obeys to the generic and
value-semantics (which are not the same) dogma.

What confuses most beginners and many experts is the mix of C++ styles
that is presented in books, articles and newsgroups without any
further discussion of the underlying 'paradigms'.
Of course, there could be Java-like containers in C++ (they would
certainly be prefered to STL containers by many real-world
programmers). Also, the procedural stdio is more usable for many cases
than the OO experiment called iostreams. Alas, fundamental design
questions simply are not conducted in C++. Hence the permanent
confusion.

Best wishes,
Roland Pibinger
Jun 22 '06 #11
In article <44*************@news.utanet.at>, rpbg123
@yahoo.com says...

[ ... ]
Of course, there could be Java-like containers in C++ (they would
certainly be prefered to STL containers by many real-world
programmers).
Spoken like somebody who either doesn't know or has long
forgotten most of the early history of C++. C++ had
containers a lot like those in Java _long_ before Java
did. If you look, you can probably still find a copy of
Keith Gorlen's ancient NIH class library, for one obvious
example.

Based on my experience and your usage, when you say
"real-world", what you really mean is "ignorant and
misguided."
Also, the procedural stdio is more usable for many cases
than the OO experiment called iostreams.
Each has strengths and weaknesses, that's true. That's
probably at least part of why the C++ standard retains
the stdio library as part of the C++ standard library.
Every library starts as an experiment, and (given time)
progresses to production use. Clearly iostreams have been
put to production use for a long time, so your
description of them as an experiment is clearly no longer
accurate.

If you don't like them, so be it. That doesn't wind the
clock back 20 years...
Alas, fundamental design
questions simply are not conducted in C++. Hence the permanent
confusion.


You seem to be the one suffering from confusion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '06 #12

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

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
3
by: alex.gman | last post by:
If I have code like this int f() { // ... stuff ... g(); if(x > 0) return (x+4); // ... more stuff ... always_call(z); return y; }
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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,...

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.