On Thu, 17 Sep 1998, holotko wrote:

> 
> Lets say I have a class "foo"and within a constructor for this class I
> wish to use "new" to allocate memory space. For example:
> 
> class foo
> {
>    private:
>       char *c;
> 
>    public:
>       foo()                                          // constructor
>        { c = new char [LEN + 1]; }
> 
> };
> 
> Of course there would be other member functions.  Now lets say I write
> a program in which I create and use objects of class foo. When I am
> finished using those objects I want to deallocate the memory which was
> allocated when the object was constructed. To accomplish this can I
> just simply write a destructor which, in it's simplest form might look
> like this:
> 
>    ~foo()
>      { delete c; }
     { delete [] c;}
is required since c is an array. 
> 
> Would this work? and, if so, how would I call this destructor when the
> object is no longer needed, or would this be done automatically at
> some point??
> 
 It depends. If you allocate the object file a file scope definitions or
on the stack, it will be automatically destroyed and the destructor will
run. If you allocated it via a new, then you are responsible for
delete-ing it.

john alvord

CDs @ http://www.cruzio.com/~billpeet/MusicByCandlelight

Reply via email to