[comp.lang.c++] Base class vs. derived class and delete

gwaters@bwdls35.bnr.ca (Glenn Waters) (03/13/91)

The following program does not do what I would hope (expect). The third line of
main does a "delete bp". This seems to only delete String _b in the base class
and *not* String _d in the derived class. Is delete not a "virtual" delete
in that it will delete _d in the derived class? I realize I could have written
"delete dp", but that is not possible when there are multiple classes derived 
from the base class.

String is a simple string class. It suffices to say that String
has a constructor and destructor.
--------
#include <C++/string.hxx>

class base {
        String _b;
public:
        base() : _b("base initial value") {};
        ~base() {};
};

class derived : public base {
        String _d;
public:
        derived() : _d("derived initializer") {};
        ~derived() {};
};

main()
{
        derived* dp = new derived;
        base* bp = d;
        delete bp;
}

-- 
Glenn Waters, Network Management Systems      gwaters@bnr.ca
Bell-Northern Research, Ltd.                  ...!uunet!bwdls35!gwaters
P.O. Box 3511, Station C                      (613)763-3933 (Voice)
Ottawa Ontario Canada K1Y 4H7                 (613)763-3283 (FAX)
#include <standard_disclaimer.h>

jgro@lia (Jeremy Grodberg) (03/15/91)

In article <1991Mar12.202624.19872@bwdls61.bnr.ca> gwaters@bwdls35.bnr.ca (Glenn Waters) writes:
>The following program does not do what I would hope (expect). The third line of
>main does a "delete bp". This seems to only delete String _b in the base class
>and *not* String _d in the derived class. Is delete not a "virtual" delete
>in that it will delete _d in the derived class?

[program omitted]

The problem is that you did not declare your destructors to be virtual.
delete calls the destructor for the object you are deleting, and if it
is not virtual, then it uses the static type.  You really should always
declare destructors to be virtual for just this reason; the only reason
destructors are not *required* to be virtual is so that you can have
classes without vtables, so as to be small and C compatable.

-- 
Jeremy Grodberg      "Show me a new widget that's bug-free, and I'll show
jgro@lia.com         you something that's been through several releases."

rmartin@clear.com (Bob Martin) (03/15/91)

In article <1991Mar12.202624.19872@bwdls61.bnr.ca> gwaters@bwdls35.bnr.ca (Glenn Waters) writes:
>The following program does not do what I would hope (expect). The third line of
>main does a "delete bp". This seems to only delete String _b in the base class
>and *not* String _d in the derived class. Is delete not a "virtual" delete
>in that it will delete _d in the derived class? I realize I could have written
>"delete dp", but that is not possible when there are multiple classes derived 
>from the base class.

 [... example deleted ....]

 This case is explained in ARM 12.4 in the discussion of virtual destructors.
 In answer to your question about "delete".  It is not "delete" which is
 virtual...  If you want virtual "deletion" then you must declare virtual
 destructors.  

-- 
+-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for |
| rmartin@clear.com    |:R::R:C::::M:M:M:M:| my words but me.  I want  |
| uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all   |
+----------------------+:R::R::CCC:M:::::M:| the blame.  So there.     |