=运算符重载和类的拷贝构造
enum enum_type {
mNone = 0, // 无效
mInt = 1, // 整形
mDouble = 2, // 浮点数
mLong = 3, // 长整形
mString = 4 // 字符串
};
class text_class
{
public:
enum_type type{ mNone };
size_t length{ 0 };
unsigned char* data{ nullptr };
// = 运算符重载
text_class& operator=(const text_class& other)
{
if (this != &other) // 避免自我赋值
{
this->type = other.type;
this->length = other.length;
// 释放原有的 data
if (this->data != nullptr)
{
delete[] this->data;
}
// 分配新的内存并复制数据
this->data = new unsigned char[other->length];
memcpy(this->data, other.data, other->length);
}
return *this;
}
// 拷贝函数
text_class(const text_class& other)
{
this->type = other.type;
this->length = other.length;
// 释放原有的 data
if (this->data != nullptr)
{
delete[] this->data;
}
this->data = new unsigned char[other.length];
memcpy(this->data, other.data, other.length);
}
// 析构
~text_class()
{
if (data != nullptr)
{
// 释放 data
delete[] data;
}
}
// 构造函数
text_class() {}
};