No.  You must explictly destroy the contained object in the
destructor of the container.  

The following code has a container bar which holds foo.  bar's
constructor new's a copy of foo, but it's destructor does not
explicitly destroy that object.
 
--[ begin code ]---

#include <stdio.h>
#include <stdlib.h>

class foo {
public:
    foo( ) { printf("constructor foo( ) called.\n"); }
   ~foo( ) { printf("destructor ~foo( ) called.\n"); }
};

class bar {
 public:
    bar( ) { printf("constructor bar( ) called.\n");
             fp = new foo( ); }

   ~bar( ) { printf("destructor ~bar( ) called.\n"); }

   foo* fp;
};

void main( ) {
   bar *bp;

   bp = new bar();

   delete bp;
}

--[ end code ]---

--[ begin output ]--
constructor bar( ) called.
constructor foo( ) called.
destructor ~bar( ) called.
--[ end output ]--

If the destructor were autmatically called, then you would see
"destructor ~foo( ) called." in the output.  You must explictly
destroy objects you create.  You can get around this by using
the auto_ptr class which will destroy pointers when the auto_ptr
object goes out of scope.

traff

( this was with egcs-1.1.1 patched up to pgcc-2.91.60 )

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Suresh
> Sent: Thursday, January 21, 1999 6:03 AM
> To: holotko
> Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: C++ Destructor Question
> 
> 
> Hi,
> The destructor is automatically called when the
> object in question is no longer needed. Such as
> an object is created inside a function locally and
> when the function returns the object gets killed with
> the destructor executed. Hence we dont call the 
> destructor explicitly.
> 
> And in java I feel the garbage collection concept is
> implemented quite strong and rarely do we use explicit
> deallocation.
> 
> Cheers,
> Suresh 
> Wipro-Nortel Networks
> Bangalore
> >
> >
> >Being spoiled by Java's garbage collector leads me to this quick
> >question again concerning constructors in C++.
> >
> >If I allocate memory via "new" using a constructor
> >
> >i.e.
> >
> >    class Foo
> >    {
> >      Foo()
> >        { word = new char[LENGTH + 1];  }
> >
> >      ~Foo()
> >        { delete word; }
> >        
> >        ...
> >     }
> >
> >When I create an object of class Foo memory will be allocated for the
> >char buffer "word". Now when the object is no longer needed must I
> >make an explicit call to the destructor ~Foo() to destroy the object
> >and subsequently call "delete", or, is the destructor somehow called
> >automatically when the object is no  longer needed,i.e.  outside of
> >it's scope?
> >
> >Even in Java there are times when it is up to you to destroy an object
> >and/or free memory used for that object, depending on how the object
> >is/was created and an method equivalent of a destructor is required...
> >The garbage collector is not always adequate.
> >
> >Thanks...
> >
> >Sincerely,
> >
> >/John <[EMAIL PROTECTED]>
> >
> >
> >-- 
> >email: [EMAIL PROTECTED]
> >Local mailserver <landreau.ruffe.edu> , remote <ns.computer.net>
> >
> >There is a great alternative to war, it's called Peace.
> >
> 
> 
> --
> 

Reply via email to