我发现您的代码存在几个问题:
-
您的默认构造函数未初始化 data。
-
您的 operator+ 未尝试将值从 this->data 和 obj.data 复制到 temp.data,因此未初始化 temp.data。
-
您的 operator+ 返回错误的 MyClass 对象。它很麻烦地准备了一个名为temp 的MyClass 对象,然后在退出时完全丢弃temp。通过将temp.size 传递给return,您将通过MyClass(int size) 构造函数创建另一个 MyClass 对象,该构造函数会生成所有新的随机数据。您需要将 return 替换为您准备的 temp 对象。然后编译器将调用您的复制构造函数将temp 分配给main() 中的c,或者将optimize away the copy 让operator+ 直接对c 进行操作。
-
根据Rule of 3,您缺少一个复制赋值运算符。您的示例中没有任何内容实际调用复制分配,但您仍然需要正确实现运算符。如果您使用 C++11 或更高版本,您还应该遵循 Rule of 5,添加移动构造函数和移动赋值运算符。但是,如果您可以将设计更改为使用std::vector 而不是new[],那么您可以按照Rule of 0 并让编译器为您完成所有艰苦的工作。
话虽如此,请尝试更多类似的东西:
#include <iostream>
#include <algorithm>
#include <utility> // C++11 and later only...
#include <cstdlib>
#include <ctime>
class MyClass
{
private:
int* data;
size_t size;
public:
MyClass(size_t size = 0) : data(NULL), size(size)
{
if (size > 0)
{
data = new int[size];
// in C++11 and later, consider using std::uniform_int_distribution instead of rand()!
std::generate(data, data + size, std::rand);
}
}
MyClass(const MyClass& obj) : data(NULL), size(obj.size)
{
if (size > 0)
{
data = new int[size];
std::copy(obj.data, obj.data + obj.size, data);
}
}
// C++11 and later only...
MyClass(MyClass&& obj) : data(NULL), size(0)
{
std::swap(size, obj.size);
std::swap(data, obj.data);
}
~MyClass()
{
delete[] data;
}
MyClass& operator=(const MyClass& rhs)
{
if (&rhs != this)
{
MyClass temp(rhs);
std::swap(size, temp.size);
std::swap(data, temp.data);
}
return *this;
}
MyClass& operator=(MyClass&& rhs)
{
MyClass temp(std::move(rhs));
std::swap(size, temp.size);
std::swap(data, temp.data);
return *this;
}
MyClass operator+(const MyClass& obj) const
{
MyClass temp;
temp.size = size + obj.size;
if (temp.size > 0)
{
temp.data = new int[temp.size];
std::copy(data, data + size, temp.data);
std::copy(obj.data, obj.data + obj.size, temp.data + size);
}
return temp;
}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
os << obj.size;
for(size_t i = 0; i < obj.size; ++i)
{
os << " " << obj.data[i];
}
return os;
}
int main()
{
std::srand(std::time(0));
MyClass a(5);
MyClass b(a);
MyClass c = a + b;
std::cout << c;
return 0;
}
可以简化为:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
class MyClass
{
private:
std::vector<int> data;
public:
MyClass(size_t size = 0) : data(size)
{
std::generate(data.begin(), data.end(), std::rand);
}
MyClass operator+(const MyClass& obj) const
{
MyClass temp;
if (!data.empty() || !obj.data.empty())
{
temp.data.reserve(data.size() + obj.data.size());
temp.data.insert(temp.data.end(), data.begin(), data.end());
temp.data.insert(temp.data.end(), obj.data.begin(), obj.data.end());
}
return temp;
}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
os << obj.size;
for(size_t i = 0; i < obj.size; ++i)
{
os << " " << obj.data[i];
}
return os;
}
int main()
{
std::srand(std::time(0));
MyClass a(5);
MyClass b(a);
MyClass c = a + b;
std::cout << c;
return 0;
}