creation of static during destruction

Discussion in 'C++' started by Parapura Rajkumar, Dec 21, 2006.

  1. hey all

    class A
    {

    };


    class B
    {
    ~B()
    {
    static A a;
    }
    };

    static B b;

    Is this undefined behavior. or the standard seems to handle this case
    as well ?

    Thanks
    Raja
    Parapura Rajkumar, Dec 21, 2006
    #1
    1. Advertising

  2. Parapura  Rajkumar

    Rolf Magnus Guest

    Parapura Rajkumar wrote:

    > hey all
    >
    > class A
    > {
    >
    > };
    >
    >
    > class B
    > {
    > ~B()
    > {
    > static A a;
    > }
    > };
    >
    > static B b;
    >
    > Is this undefined behavior.


    No. It's an error. B cannot be instantiated.
    Rolf Magnus, Dec 21, 2006
    #2
    1. Advertising

  3. Parapura Rajkumar wrote:
    > class A
    > {
    >
    > };
    >
    >
    > class B
    > {
    > ~B()
    > {
    > static A a;
    > }
    > };
    >
    > static B b;
    >
    > Is this undefined behavior. or the standard seems to handle this case
    > as well ?


    I think it's unspecified.

    The Standard (as I read it) says that the destruction of 'a' will
    happen before B::~B returns (since it was constructed after 'b',
    it has to be destroyed completely before 'b' is), however, it is known
    that calling d-tors for static objects is done by registering them
    with 'atexit' (although it is not explicitly specified by the Standard),
    and the Standard does not specify what happens if you [try to] register
    a function with 'atexit' while another function previously registered
    with 'atexit' is executed during the program termination.

    You might want to ask about it in comp.std.c++, refer to 3.6.3, 18.3,
    6.7.

    I say, try to avoid situations like that if possible.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 21, 2006
    #3
  4. Rolf Magnus wrote:
    > Parapura Rajkumar wrote:
    >
    > > hey all
    > >
    > > class A
    > > {
    > >
    > > };
    > >
    > >
    > > class B
    > > {
    > > ~B()
    > > {
    > > static A a;
    > > }
    > > };
    > >
    > > static B b;
    > >
    > > Is this undefined behavior.

    >
    > No. It's an error. B cannot be instantiated.


    Why is that.. i omitted the public to keep the post terse
    Parapura Rajkumar, Dec 21, 2006
    #4
  5. Rolf Magnus wrote:
    > Parapura Rajkumar wrote:
    >
    >> hey all
    >>
    >> class A
    >> {
    >>
    >> };
    >>
    >>
    >> class B
    >> {
    >> ~B()
    >> {
    >> static A a;
    >> }
    >> };
    >>
    >> static B b;
    >>
    >> Is this undefined behavior.

    >
    > No. It's an error. B cannot be instantiated.


    Oh, come on!
    Victor Bazarov, Dec 21, 2006
    #5
  6. Parapura  Rajkumar

    Yahooooooooo Guest

    Parapura Rajkumar wrote:
    > hey all
    >
    > class A
    > {
    >
    > };
    >
    >
    > class B
    > {
    > ~B()
    > {
    > static A a;
    > }
    > };
    >
    > static B b;
    >
    > Is this undefined behavior. or the standard seems to handle this case
    > as well ?
    >
    > Thanks
    > Raja



    what is the use of this......B destructor will be called when B process
    is complete...
    Yahooooooooo, Dec 22, 2006
    #6
    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. ian douglas
    Replies:
    0
    Views:
    1,811
    ian douglas
    Aug 19, 2003
  2. Miguel Guedes

    Creation and destruction responsibilities

    Miguel Guedes, Sep 4, 2007, in forum: C Programming
    Replies:
    2
    Views:
    267
    Miguel Guedes
    Sep 4, 2007
  3. Miguel Guedes
    Replies:
    2
    Views:
    233
    James Kanze
    Sep 4, 2007
  4. Victor Bazarov
    Replies:
    11
    Views:
    738
    James Kanze
    Dec 9, 2009
  5. jacquesh
    Replies:
    0
    Views:
    387
    jacquesh
    Aug 23, 2010
Loading...

Share This Page