Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::exception_detail::error_info_base does not have virtual destructor
From: Ted Byers (r.ted.byers_at_[hidden])
Date: 2011-03-17 15:27:55


>>>>
>>>> IMHO these classes should have a virtual destructor as they are
>>>> intended
>> to be used as base classes.
>>>
>>> No they shouldn't. A virtual destructor is only needed when deleting
>>> through a base class pointer. This is explicitly forbidden by making
>>> the destructor
>>> protected.
>>
>> I must respectfully disagree.
>>
>> There are two ways to look at this 'should'. First, there is what is
>> required or permitted by the standard. Second, there is what is
>> useful in managing a software development project. When I am trying
>> to help my juniors develop good programming habits, one of the things
>> I want them to do is always provide a virtual destructor whenever they
>> make one or more member functions virtual. That lets a team comprised
>> of many more junior programmers than really really old guys like me avoid
a lot of subtle bugs.
>> And I don't see a significant down side to making a destructor virtual.
>> Hence, when I am mentoring junior programmers, I routinely advise them
>> to make destructors virtual whenever they need one of the other member
>> functions to be virtual, whenever the issue arises.
>>
>> Are you aware of a significant downside to making destructors virtual
>> as a matter of habit?
>Why not just encourage the habit of making *public* destructors virtual in
the presence of one or more other virtual methods? That is, after all, when
a virtual destructor can be necessary.

Because I tend to let junior programmeers work only on those classes that
would have a public destructor anyway. I find young, inexperienced
programmers need to develop a good grounding on, or experience with,
generally applicable patterns before being introduced to situations where
one can usefully deviate from those patterns. I have seen kids graduate
from college programs where they have had only one one semester course in
C++ (and they then call themselves C++ programmers). When dealing with such
kids, I really don't want to present them with everything they can do with
the language. Pushing such kids too far too fast tends only to leave them
frustrated and discouraged, and we then risk losing young people with little
experience but tonnes of potential. So, I give them a general 'rule', and
then only when they have enough experience to I take them to the next level
of "you do not have to do this of you are going to do this other thing",
i.e. introduce exceptions to the rule. There are only to ways of getting
good programmeers: hire them away from other companies or train them in
house (and if you opt for the latter, you have to adapt to where they are,
rather than what you would expect from fine institutions like MIT).

Again, can you point to a significant downside to having all destructors
virtual?

Cheers,

Ted


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net