In article <>,
holotko <[EMAIL PROTECTED]> wrote:
>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.
You need to destroy the object when you have allocated it on the heap.
You need to do the reference counting yourself (or use a reference
class that does it automatically).
class* Foo foop = new Foo;
// blah blah blah
delete foop;
You don't call ~Foo explicitly but you do explicitly destroy the object.
--
Zygo Blaxell (with a name like that, who needs a nick?)
Linux Engineer (my favorite official job title so far)
Corel Corporation (whose opinions sometimes differ from those shown above)
[EMAIL PROTECTED] (also [EMAIL PROTECTED])