Circular dependency in constructor initialization list

杀马特。学长 韩版系。学妹 提交于 2019-12-10 14:24:11

问题


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.



来源:https://stackoverflow.com/questions/29809241/circular-dependency-in-constructor-initialization-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!