Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about object destruction

Reply
Thread Tools

question about object destruction

 
 
johny smith
Guest
Posts: n/a
 
      05-09-2004
Suppose there is a policy that all objects are statically declared.

For example:

static Car car();

Then, is there a reason to have a destructor defined for the class Car.

It would seem that perhaps there would not be a need for a destructor
because the object would never go out of scope due to the static storage.

Any insight?

Many thanks


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      05-09-2004
On Sun, 9 May 2004 16:33:57 -0600 in comp.lang.c++, "johny smith"
<> wrote,
>Suppose there is a policy that all objects are statically declared.
>
>For example:
>
>static Car car();


That's not an object. It's a forward declaration of a function with no
arguments returning Car.

>It would seem that perhaps there would not be a need for a destructor
>because the object would never go out of scope due to the static storage.


Static objects are destructed at the end of execution. If you are doing
nothing special, then the default compiler generated destructor may be
all you need.


 
Reply With Quote
 
 
 
 
Juergen Heinzl
Guest
Posts: n/a
 
      05-09-2004
In article <>, johny smith wrote:
> Suppose there is a policy that all objects are statically declared.
>
> For example:
>
> static Car car();
>
> Then, is there a reason to have a destructor defined for the class Car.

[-]
Sometimes.

> It would seem that perhaps there would not be a need for a destructor
> because the object would never go out of scope due to the static storage.

[-]
class static_shm_pointer {
public :
static_shm_pointer( size_t shm_size ) {
/*
** get shared memory
*/
}

~static_shm_pointer() {
/*
** remove shared memory
*/
}
};

void function() {
/*
** time consuming, so do it just once
*/
static static_shm_ptr( 1234 );
}

Cheers,
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : \ send money instead /
\ Photo gallery : www.manannan.org \ /
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-10-2004

"johny smith" <> wrote in message
news:...
> Suppose there is a policy that all objects are statically declared.
>
> For example:
>
> static Car car();


static Car car;

>
> Then, is there a reason to have a destructor defined for the class Car.
>
> It would seem that perhaps there would not be a need for a destructor
> because the object would never go out of scope due to the static storage.
>
> Any insight?


Sometimes the operating system doesn't not automatically recover resources
not released by the program. For instance it might be that if a file is
opened and not closed by a program, then the file will remain open (and
perhaps therefore unusable by any other program) even though the program
that opened the file has exitted.

So in the case of an object which opened a file you would still need a
destructor to close the file.

It's true that most operating systems will recover all the memory used by a
program, but this isn't guaranteed either so its still sensible to have a
destructor.

Finally even if you aren't concerned about the above, its not normally good
design to build into an object the assumtpion that it must be allocated
statically.

john


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-10-2004
johny smith wrote:
> ...
> It would seem that perhaps there would not be a need for a destructor
> because the object would never go out of scope due to the static storage.
> ...


I has nothing to do with object's scope, but it has everything to do
with object's lifetime. Destructor is called when object's lifetime
ends. Every object's lifetime ends sooner or later, including objects
with static storage duration. In case of objects with static storage
duration the lifetime ends at the end of the program. That's when the
destructors for such objects are called. I don't see why you'd say that
there's no need for them.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ faq-lite 11.10 placement new object destruction Fei Liu C++ 3 04-25-2007 09:02 PM
scope and object destruction kalki70 C++ 2 02-07-2007 08:31 PM
Object Destruction with / without Virtual Function V Patel C++ 5 01-30-2007 06:50 AM
Help with destruction of an object created with the new operator- destructors Pablo C++ 6 08-18-2006 04:14 PM
object destruction EN C++ 3 03-15-2005 12:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57