|| | |||Browse by category|
http://www.kbpublisher.com - http://www.kbpublisher.com
[b]text[/b] - Bold text.
[u]text[/u] - Underline text.
[i]text[/i] - Italic text.
[color=green]text[/color] - Colored text.
[url]kbpublisher.com[/url] - kbpublisher.com
[url=kbpublisher.com]text[/url] - text
[email]firstname.lastname@example.org[/email] - email@example.com
[h1]text[/h1] - Caption text.
- item 1
- item 2
- item 1
- item 2
The use of non-virtual destructors in various Tools.h++ classes may cause problems when they are used as base classes.
The use of virtual destructors is a design decision. So, how does one know when to use virtual destructors? According to Meyers1, "The bottom line is that gratuitously declaring all destructors virtual is just as wrong as never declaring them virtual". In fact, many people summarize the situation this way: declare a virtual destructor in a class if and only if that class contains at least one virtual function. The downside to this rule is that it is possible to create problems by declaring a nonvirtual destructor even if there are no virtual functions in the same class. Cargill2 suggests that if a derived class or a member of a derived class does define a destructor, and the base destructor remains non-virtual, memory leaks or other abnormalities can ensue.
According to Koenig3, an implicitly-declared destructor is implicitly defined when it is used to destroy an object of its class type (_basic.stc_). A program is ill-formed if the class for which a destructor is implicitly defined has:
- --a non-static data member of class type (or array thereof) with an inaccessible destructor, or
- --a base class with an inaccessible destructor.
Since RWCString is a base class with no virtual destructor and no virtual functions, it is simply a base class with an inaccessible destructor. Therefore, the program may be considered ill-formed. However, one possible explanation4 for the exclusion of a virtual destructor is the fact that RWCString was not intended to be used as a base class and that RWCString was designed several years ago when memory was still an issue for many software developers. The idea was to make RWCString small enough to fit into a 32-bit register and adding the vtbl (created for use with virtual functions, including destructors) would have made this impossible. A design specification such as this one may not be suitable for all users in all situations. For many of the Tools.h++ classes, adding virtual destructors would violate the principle design of many of the Tools classes (which is extreme simplicity, but little generality). There are many examples of classes without virtual destructors in the realm of classes that extend the set of primitive types.
Footnotes and Bibliography
- Meyers, Scott, Effective C++, 2nd Edition, Item #14, pp. 59-64, Addison Wesley, 1998
- Cargill, Tom, C++ Programming Style, pg. 78, Addison Wesley, 1992
- Koenig, Andrew, ANSI ISO Doc No. X3J16/96-0225, WG21/N1043, Programming Language C++, Chapter 12 (Section 12.4.5 on Destructors), 2 December 1996. (See also 12.4.6, 12.4.10, 12.4.12, and 12.4.12)
- Dennis Kennedy, Development, RogueWave North America