2つの可能性があります-インライン:
class DerivedClass : public BaseClass {
public:
DerivedClass (string b) : BaseClass(b) {}
};
またはライン外:
class DerivedClass : public BaseClass {
public:
DerivedClass (string b);
};
/* ... */
DerivedClass::DerivedClass(string b) : BaseClass(b)
{}
その他の例:
class DerivedClass : public BaseClass {
public:
DerivedClass(int a, string b, string c);
private:
int x;
};
DerivedClass::DerivedClass(int a, string b, string c) : BaseClass(b + c), x(a)
{}
初期化子リスト:
class MyType {
public:
MyType(int val) { myVal = val; } // needs int
private:
int myVal;
};
class DerivedClass : public BaseClass {
public:
DerivedClass(int a, string b) : BaseClass(b)
{ x = a; } // error, this tries to assign 'a' to default-constructed 'x'
// but MyType doesn't have default constructor
DerivedClass(int a, string b) : BaseClass(b), x(a)
{} // this is the way to do it
private:
MyType x;
};