为什么析构函数在行之后被调用mystring = "Hello, there!";
呢?它不像它已经超出范围了.我肯定错过了一些C++的怪癖!
我知道该行调用默认的复制构造函数,是复制构造函数返回后总是调用的析构函数吗?
正如旁注,我正在使用C++ 03,还没有使用C++ 11.
编辑:另请注意,我知道以下程序导致的双重删除.我在这里做实验.只是想提醒你注意.
class MyString
{
private:
char* mystring;
int m_length;
public:
MyString(const char* str="")
{
assert(str);
m_length = strlen(str) + 1;
mystring = new char[m_length];
for (int i = 0; i < m_length; ++i)
mystring[i] = str[i];
mystring[m_length-1] = '\0';
}
~MyString()
{
delete[] mystring;
}
};
int main()
{
MyString mystring("");
mystring = "Hello, there!";
cout << "Destructor not yet called ";
}
Run Code Online (Sandbox Code Playgroud)
由于您的类没有赋值运算符,因此将字符串文字mystring = "Hello, there!";转换为3部分操作.
首先,它必须从字符串文字构造一个临时对象.
然后它需要临时并使用它与默认副本(前C++ 11)/ move(如果没有删除,则发布C++ 11)编译器为类生成的赋值运算符.
然后它必须销毁它创建的临时对象.这就是为什么你会在该表达式的末尾看到对析构函数的调用.
请注意,因为临时对象在之后被销毁
mystring = "Hello, there!";
Run Code Online (Sandbox Code Playgroud)
指针mystring保持的现在已删除,您无法再访问它.当它被破坏时它也会导致双重删除,这是一种不确定的行为,会引起并发症.