它类似于:
template <typename T, int N>
struct array
{
T c_arr[N];
};
我想知道为什么在这种情况下自动生成的operator =支持深层复制?
std::array<int, 3> a1{1, 2, 3}, a2;
a2 = a1;
//all the elements in a1.c_arr have been copied to a2.c_arr
copy(begin(a2), end(a2), ostream_iterator<int>(cout,"\t"));
output:
1 2 3
如果我们定义一个没有用户定义的operator =的类,它也支持深层复制。
struct Test
{
int a[3];
};
Test a1{1, 2, 3}, a2;
a2 = a1;
//all the elements in a1.a have been copied to a2.a
copy(begin(a2), end(a2), ostream_iterator<int>(cout,"\t"));
output:
1 2 3
答案 0 :(得分:0)
std::begin()
实例上的 std::end()
和std::array
返回std::array
内实际数组的开头和结束迭代器。
我不明白为什么复制构造函数与此有关。我看到在这里没有任何复制结构。
答案 1 :(得分:0)
因为它是如何运作的。这是唯一可能有意义的行为,除了无所事事或是编译器错误。
C样式数组是由多个元素类型实例组成的聚合 - 特别是不是指针类型。