-->

Circular dependency in constructor initialization

2019-06-22 19:22发布

问题:

Is the following well-defined?

class A;
class B;

// define A, which takes B& in constructor
// define B, which takes A& in constructor

class C
{
    A a;
    B b;
public:
    C() : a(b), b(a) { /* stuff with a and b */ }
}

Full example at ideone.com.

Is it safe/well-defined so long as the constructors for A and B don't do anything with the references they get?

回答1:

N4140 [class.cdtor]/1 reads:

For an object with a non-trivial constructor, referring to any non-static member or base class of the object before the constructor begins execution results in undefined behavior. For an object with a non-trivial destructor, referring to any non-static member or base class of the object after the destructor finishes execution results in undefined behavior.

While this passage itself doesn't imply that the behavior is otherwise well-defined, the following example shows that it is. Here is an excerpt:

struct B : public A { int j; Y y; }; // non-trivial
extern B bobj;
B* pb = &bobj; // OK

So the answer is: yes, the behavior in your case is well defined if you aren't referring to members or base classes of b in the constructor of A.