数组的复制和赋值构造函数C++示例
问题描述:
需要一些帮助来为我的代码编写复制和赋值构造函数。我收到一个错误“一个数组只能用一个初始化列表初始化”。感谢您的帮助 - 谢谢!数组的复制和赋值构造函数C++示例
class B
{
public:
C **table;
B()
{
table = new C *[TABLE_SIZE]();
}
B(const B& other)
{
table = new C *[TABLE_SIZE](other.table);
memcpy(table, other.table, sizeof(C *)* TABLE_SIZE);
}
B& operator = (const B& other)
{
if (this == &other)
{
return *this;
}
delete[] table;
table = new C *[TABLE_SIZE](other.table);
memcpy(table, other.table, sizeof(C *)* TABLE_SIZE);
return *this;
}
}
答
我猜测这是因为你在初始化table
方式:
table = new C *[TABLE_SIZE](other.table);
memcpy(table, other.table, sizeof(C *)* TABLE_SIZE);
试试这个INSEAD:
table = new C *[TABLE_SIZE];
memcpy(table, other.table, sizeof(C *)* TABLE_SIZE);
我真的不明白,你为什么会想初始化数组的值,因为这就是你的memcpy
。
请格式化您的代码,使其可读。谢谢。 – 2015-03-18 23:13:38