// Use multiple inheritance. We want
// both a string and an Object:
class MyString: public string, public Object {
public:
~MyString() {
cout << "deleting string: " << *this << endl;
}
MyString(string s) : string(s) {}
};
對於上面的代碼,我不明白什麼string(s)
是什麼意思?實際上並沒有稱爲string
的變量,但爲什麼它可以工作?這個初始化器列表是什麼意思?
[在構造函數中這個奇怪的冒號成員語法是什麼?](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the -constructor) – 2013-03-13 14:51:17
這不是重複的。您鏈接的問題是關於成員初始化,在這種情況下,調用基類構造函數。正如OP提到的那樣,沒有成員稱爲字符串。 – JSQuareD 2013-03-13 15:04:47
只是說,你可能不想從'string'派生。 – 2013-03-13 15:11:52