我正在阅读 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;
我没有列出完整的代码。如果运行完整的代码,输出是:
删除字符串:你好。
这意味着 *this = "你好"。