2016-09-06 5 views
1

のシーケンス継承と初期化

class A 
    { 
    }; 

class B 
    { 
    B(A * in); 
    }; 

class C : public A 
    { 
    B b; 
    public: 
    C():b(this){} 
    }; 

を考えるには、C安全でコンストラクタですか? Aのメンバーは既に利用可能です(そして構築されていますか?

+0

"安全" の定義.. –

答えて

4

はい。すべての基本クラスは、コンストラクターの残りの部分が実行される前に構築されます。例えば

、Stroustrup氏C++ 2011本から:

17.2.3 Base and Member Destructors 
Constructors and destructors interact correctly with class hierarchies (§3.2.4, Chapter 20). A constructor 
builds a class object ‘‘from the bottom up’’: 
[1] first, the constructor invokes its base class constructors, 
[2] then, it invokes the member constructors, and 
[3] finally, it executes its own body. 
A destructor ‘‘tears down’’ an object in the reverse order: 
[1] first, the destructor executes its own body, 
[2] then, it invokes its member destructors, and 
[3] finally, it inv okes its base class destructors. 
+1

'B'のコンストラクタはin' 'に' virtual'機能を起動しようとしたときを除きます。 – LogicStuff

関連する問題