Re: C++: destructor and delete
Newsnet Customer wrote:
[color=blue]
> ...
> Statement 1: "A dynamically created local object will call it's destructor
> method when it goes out of scope when a procedure returms"
>
> Agree.[/color]
The question makes no sense. Local objects, by definition, cannot be
dynamically created in C++. Local object can be either automatic or
static. Actually, the standard is rather ambiguous on whether locally
defined static objects can be referred to as "local", but in no case a
dynamically created object can be referred to as "local".
[color=blue]
>
> Statement 2: "A dynamically created object will call it's destructor when it
> is made a target of a delete".[/color]
This question also makes no sense. Nowhere in C++ specification it says
that the object is responsible for calling its own destructor. The
destructor is called when the object is destroyed. Who performs the call
- no one knows.
OK, let's assume that the object being destroyed calls its own destructor.
[color=blue]
> Does not make sense to me. Would not this result in an endless loop? (ie.
> delete calls destructor and within destructor it calls delete, which in turn
> calls the destructor again and so on....)[/color]
No there's no endless loop here. Object's destructor doesn't call
'delete'. What makes you think it does?
--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
Newsnet Customer wrote:
[color=blue]
> ...
> Statement 1: "A dynamically created local object will call it's destructor
> method when it goes out of scope when a procedure returms"
>
> Agree.[/color]
The question makes no sense. Local objects, by definition, cannot be
dynamically created in C++. Local object can be either automatic or
static. Actually, the standard is rather ambiguous on whether locally
defined static objects can be referred to as "local", but in no case a
dynamically created object can be referred to as "local".
[color=blue]
>
> Statement 2: "A dynamically created object will call it's destructor when it
> is made a target of a delete".[/color]
This question also makes no sense. Nowhere in C++ specification it says
that the object is responsible for calling its own destructor. The
destructor is called when the object is destroyed. Who performs the call
- no one knows.
OK, let's assume that the object being destroyed calls its own destructor.
[color=blue]
> Does not make sense to me. Would not this result in an endless loop? (ie.
> delete calls destructor and within destructor it calls delete, which in turn
> calls the destructor again and so on....)[/color]
No there's no endless loop here. Object's destructor doesn't call
'delete'. What makes you think it does?
--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
Comment