我正在閱讀C++中的思考。我對該書中顯示的以下代碼片段感到困惑。繼承自C++中的字符串
class MyString: public string, public Object {
public:
~MyString() {
cout << "deleting string: " << *this << endl;
}
MyString(string s) : string(s) {}
};
我很困惑*this
。 this
是一個指向MyString對象本身的指針。 爲什麼*this
作爲初始化字符串解除引用? 爲了使我的問題更清楚,我舉一個例子:
Mystring* x = new string("Hello");
delete x;
我不列出完整的代碼。如果運行完整代碼,則輸出爲:
刪除字符串:hello。
這意味着*這=「你好」。
不是一個好主意,而且該代碼不應該編譯。 – chris
不要從'std :: string'公開繼承。 –
你的問題是什麼? –