这片土地的旧时光 · 详解 C++98 与新版 C++ 的不同
// C++98 风格:手动管理内存
class OldStyle
{
int* data;
public:
OldStyle() : data(new int[100]) {}
~OldStyle() { delete[] data; }
// 必须手动实现拷贝构造和赋值,否则会 double free!
OldStyle(const OldStyle& other) : data(new int[100])
{
for (int i = 0; i < 100; ++i)
data[i] = other.data[i];
}
OldStyle& operator=(const OldStyle& other)
{
if (this != &other)
{
delete[] data;
data = new int[100];
for (int i = 0; i < 100; ++i)
data[i] = other.data[i];
}
return *this;
}
};特性
C++98
现代 C++
Last updated