我知道有更好的选择,例如 a std::vector
orstd::array
等。就我而言,我必须使用指向动态分配数组的指针,因为我正在学习复制和交换习语,它涉及创建我自己的资源管理类。
假设我有一个资源句柄类的以下复制构造函数:
A(const A& other) : size(other.size), arr(size ? new int[size]() : nullptr) {
std::copy(other.arr, other.arr + size, arr);
}
它不能在 Visual Studio(2013 Preview 和 2012 Express)中编译。我得到的错误是:
是否可以以std::copy
另一种方式使用,以便编译器停止对我大喊大叫?或者使用简单的循环手动复制数组的内容是否更好
for (int i = 0; i < size; i++)
arr[i] = other.arr[i];
PS我不想使用任何黑客/宏来禁用警告等。