Setting member variables before constructor call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • syntheticpp@yahoo.com

    #1

    Setting member variables before constructor call

    Is the program at the end a correct C++ program?

    I ask because the output is:

    B::operator A&()
    A::A()
    B::B()
    B::~B()
    A::~A()

    that means the casting operator which sets the
    member variable B::p is called before the constructor
    of B.


    Here the program:


    #include <iostream>

    using std::cout;

    struct A
    {
    A(){ cout <<"A::A()\n"; }
    ~A(){ cout <<"A::~A()\n" ; }
    };

    struct B
    {
    B() // : p(0) // this kills the desruction of p
    { cout <<"B::B()\n"; }

    ~B()
    {
    cout <<"B::~B()\n" ;
    delete p;
    }

    A* p;

    operator A&()
    {
    cout <<"B::operato r A&()\n";
    p = new A;
    return *p;
    }
    };

    struct C
    {
    A& a;
    B b;

    C() : a(b) {}
    };

    int main()
    {
    C* c = new C;
    delete c;

    #if defined(__BORLA NDC__) || defined(_MSC_VE R)
    system("PAUSE") ;
    #endif

    }

  • Victor Bazarov

    #2
    Re: Setting member variables before constructor call

    syntheticpp@yah oo.com wrote:[color=blue]
    > Is the program at the end a correct C++ program?[/color]

    A member function is called for an object whose lifetime hasn't started
    yet because it's not been constructed. The storage the object occupies
    is already create at that time. A pointer to that storage can be used,
    but that use is limited. Dereferencing that pointer to access any member
    of the object causes the program to have undefined behaviour. In your
    case you attempt to assign the 'p' member inside B::operator A&(). Due
    to that, your program has undefined behaviour.

    It is a correct C++ program whose behaviour is undefined.
    [color=blue]
    > I ask because the output is:
    >
    > B::operator A&()
    > A::A()
    > B::B()
    > B::~B()
    > A::~A()
    >
    > that means the casting operator which sets the
    > member variable B::p is called before the constructor
    > of B.
    >
    >
    > Here the program:
    >
    >
    > #include <iostream>
    >
    > using std::cout;
    >
    > struct A
    > {
    > A(){ cout <<"A::A()\n"; }
    > ~A(){ cout <<"A::~A()\n" ; }
    > };
    >
    > struct B
    > {
    > B() // : p(0) // this kills the desruction of p
    > { cout <<"B::B()\n"; }
    >
    > ~B()
    > {
    > cout <<"B::~B()\n" ;
    > delete p;
    > }
    >
    > A* p;
    >
    > operator A&()
    > {
    > cout <<"B::operato r A&()\n";
    > p = new A;[/color]
    ^^^^
    This is the cause of the undefined behaviour.
    [color=blue]
    > return *p;
    > }
    > };
    >
    > struct C
    > {
    > A& a;
    > B b;
    >
    > C() : a(b) {}[/color]

    It is generally a BAD IDEA(tm) to use uninitialised or only partially
    constructed objects in the constructor initialisation list.
    [color=blue]
    > };
    >
    > int main()
    > {
    > C* c = new C;
    > delete c;
    >
    > #if defined(__BORLA NDC__) || defined(_MSC_VE R)
    > system("PAUSE") ;
    > #endif
    >
    > }
    >[/color]

    V

    Comment

    • syntheticpp@yahoo.com

      #3
      Re: Setting member variables before constructor call

      I've assumed such a answer.
      One way to circumvent the undefined behaviour is to use inheritance,
      but is there a way to initialize the refernce without using
      inheritance?
      Peter

      Comment

      • syntheticpp@yahoo.com

        #4
        Re: Setting member variables before constructor call


        Victor Bazarov schrieb:[color=blue]
        > A member function is called for an object whose lifetime hasn't started
        > yet because it's not been constructed. The storage the object occupies
        > is already create at that time.[/color]

        But why is it then dangerous to use this storage?

        Comment

        • Victor Bazarov

          #5
          Re: Setting member variables before constructor call

          syntheticpp@yah oo.com wrote:[color=blue]
          > I've assumed such a answer.
          > One way to circumvent the undefined behaviour is to use inheritance,
          > but is there a way to initialize the refernce without using
          > inheritance?[/color]

          I think you need to rearrange the members. Your 'b' will have been
          already initialised at the time when you initialise 'a'.

          V

          Comment

          • Victor Bazarov

            #6
            Re: Setting member variables before constructor call

            syntheticpp@yah oo.com wrote:[color=blue]
            > Victor Bazarov schrieb:
            >[color=green]
            >>A member function is called for an object whose lifetime hasn't started
            >>yet because it's not been constructed. The storage the object occupies
            >>is already create at that time.[/color]
            >
            >
            > But why is it then dangerous to use this storage?[/color]

            "Use" is a very vague term. What do you mean by "use"? It's allowed to
            pass the pointer to that storage elsewhere, and even store that pointer
            in another object, for example, since the storage is not going away. I
            don't want to quote the entire subclause of the standard, if you want to
            see what is disallowed, please look at "3.8 Object Lifetime".

            If you'd like a speculation on the subject of possible problems you can
            run into, then imagine that you're trying to access a member of a class
            that adjusts its own layout when initialising. There is no layout at all
            when an object is still under construction, even if the compiler will let
            you access a member, it's possible that the address (or the offset) it
            will use is incorrect since the initialisation proper hasn't yet been
            completed. So, you'll write something in the wrong place where it either
            gets overridden or worse where your action overrides some other value,
            which the program needs. That's just one example.

            V

            Comment

            • syntheticpp@yahoo.com

              #7
              Re: Setting member variables before constructor call

              It depends on the declaration order:

              B b;
              A& a;

              solves the problem.

              Comment

              Working...