Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私の理解が正しければ、基本クラスのコンストラクターは常に派生クラスのオブジェクトを作成するときに呼び出されます。派生オブジェクトを作成するときに、オーバーロードされた基本クラスのコンストラクターを呼び出す方法はありますか?
はい、初期化リストを介して:
class Base { public: Base (int n) : mN(n) {} private: int mN; }; class Derived : public Base { public: Derived() : Base (42) {}; // ^^^^^^^^^^^ // Initialization List };
初期化リストの構文の詳細については、次の質問を参照してください。
コンストラクターのこの奇妙なコロンメンバー (" : ") 構文は何ですか?