question about object destruction

Discussion in 'C++' started by johny smith, May 9, 2004.

  1. johny smith

    johny smith Guest

    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:)
     
    johny smith, May 9, 2004
    #1
    1. Advertising

  2. johny smith

    David Harmon Guest

    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.
     
    David Harmon, May 9, 2004
    #2
    1. Advertising

  3. 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 \ /
     
    Juergen Heinzl, May 10, 2004
    #3
  4. "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
     
    John Harrison, May 10, 2004
    #4
  5. 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
     
    Andrey Tarasevich, May 10, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EN

    object destruction

    EN, Mar 15, 2005, in forum: C++
    Replies:
    3
    Views:
    339
    Leon Mergen
    Mar 15, 2005
  2. Pablo
    Replies:
    6
    Views:
    345
    Howard
    Aug 18, 2006
  3. V Patel
    Replies:
    5
    Views:
    321
    Yahooooooooo
    Jan 30, 2007
  4. kalki70

    scope and object destruction

    kalki70, Feb 7, 2007, in forum: C++
    Replies:
    2
    Views:
    322
    kalki70
    Feb 7, 2007
  5. Fei Liu
    Replies:
    3
    Views:
    561
    Roland Pibinger
    Apr 25, 2007
Loading...

Share This Page