class Human
{
protected:
string name;
public:
Human () : name ("Jim") {}
Human (string n) : name (n) {}
};
class Adult : public Human
{
private:
string passportId;
public:
Adult ()// : name ("Eric"), passportId ("N0123") - *THIS IS ERROR*
{
// this is ok
name = "Eric";
passportId = "N0934956";
}
Adult (string n, string id)// : name(n), passportId(id) *THIS IS ERROR*
{
// this is ok
name = n;
passportId = id;
}
};
所以我们有基类Human和派生类Adult。在代码中(构造函数的实现在哪里)你可以看到注释行。
为什么在这种情况下使用这样的初始化是错误的?
Adult (string n, string id) : name(n), passportId(id) {} // *THIS IS ERROR*