Re: Odd behavior, vector member, MFC and consol app

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Good points. I was concerned that the storage problem might be due exactly to having
someone call the base class destructor which then ended up doing something bad.
joe

On Sun, 12 Aug 2007 12:46:34 -0500, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

On Sun, 12 Aug 2007 00:16:26 -0400, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

Note that destructors should also be virtual.
joe

... if they need to be. The only reason to make a destructor virtual is to
allow a derived class object to be deleted through a pointer to a base
class subobject. For this to work, the base class in question must have a
virtual destructor. If a class is not intended for derivation (1), such as
CString or std::vector, it should have no virtual functions at all. If a
class is meant to be derived from as a pure composition technique, such as
boost::noncopyable and std::iterator, it should not have a virtual dtor
(2).

(1) A good clue that a class is not intended for derivation is the absence
of any virtual function.

(2) An exception to (1) is (2). The boost::noncopyable class is really a
workaround for the lack of something like the CLR's "attributes" in C++,
while std::iterator just declares a bunch of typedefs that ease iterator
definition. To be useful, you must derive from them, but it's not
derivation for OOP reasons.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Odd behavior, vector member, MFC and consol app
    ... The only reason to make a destructor virtual is to ... the base class in question must have a ... it should have no virtual functions at all. ... A good clue that a class is not intended for derivation is the absence ...
    (microsoft.public.vc.mfc)
  • Re: virtual destructor
    ... >> A pure virtual destructor is sometimes used to make an abstract class, ... >> when there are no other virtual functions in that base class than make ... You also still provide the destructor implementation ... The subclass version can simply call the base class version. ...
    (comp.lang.cpp)
  • Re: RAII for value objects
    ... > method_pair and its virtual functions are unnecessary because, ... I try to express the RAII design pattern/idiom in C++ language terms. ... destruction as an implementation detail that should not be visible to ... destructor. ...
    (comp.lang.cpp)
  • Re: Issue with virtual functions and CThread
    ... > I have a base class that is derived from CThread. ... > are several virtual functions that contain default implementations. ... > I have a second class that derives from X, ... while I stop and restart my debugging session I ...
    (comp.sys.mac.programmer.help)
  • Re: Virtual destructors are unique virtuals right
    ... The destructor of a derived class does not call the ... >> of its base class, regardless of whether the base class has a virtual ... constructor for the BaseClass, then calls the constructor for the ... DerivedClass. ...
    (comp.lang.cpp)