By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

question about object destruction

P: n/a
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:)
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 9 May 2004 16:33:57 -0600 in comp.lang.c++, "johny smith"
<pr**************@charter.net> 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.
Jul 22 '05 #2

P: n/a
In article <10*************@corp.supernews.com>, 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 : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Jul 22 '05 #3

P: n/a

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
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
Jul 22 '05 #4

P: n/a
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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.