Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency in constructor initialization list

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?

like image 692
Claudiu Avatar asked Nov 01 '22 06:11

Claudiu


1 Answers

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.

like image 81
Anton Savin Avatar answered Nov 13 '22 04:11

Anton Savin