与数组成员默认拷贝赋值 [英] Default copy assignment with array members

查看:275
本文介绍了与数组成员默认拷贝赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似下面的类定义:

 类UUID
{
  上市:
    //使用隐含的拷贝赋值运算符  私人的:
    无符号字符缓冲区[16];
};

我刚刚有了一个单元测试失败对我这是验证了拷贝赋值工作正常。令我惊讶的是,在缓冲区[]数组中间的一个字节被错误地复制。

我的理解是,默认的拷贝赋值运算符执行成员复制,这对于阵列成员(不指针到数组成员)是引起该数组按元素副本。是我错了?

我的直觉感觉这里就是我一直悬空指针的地方,对我的数组的中间跺着脚咬伤。但是,我重复地看到了这个时候,例如我复制这些物体的向量到另一个向量。

有人关心地告诉我,我已经去了?

编辑:

要扩大这个有点,班上没有一个POD类型 - 它从几抽象基类派生,因而具有虚析构函数。然而,该数组是唯一的数据成员,并打破了在单元测试中的使用是这样的:

  const int的N = 100;的std ::矢量<&UUID GT; SRC,DST;
src.reserve(N);
dst.resize(N);的for(int i = 0; I< N ++我){
  UUID ID;
  src.push_back(ID);
}的for(int i = 0; I< N ++我){
  DST由[i] = SRC [I]
}布尔好= TRUE;
的for(int i = 0; I< N ++我){
  常量布尔thisGood =(DST [I] == SRC [I]);  性病::法院LT&;< 我=<< I<< - > SRC ='<< SRC [I]
            << ',DST ='<< DST [1] - ;&下; ',SRC == DST?
            << thisGood<<的'\\ n';  好=(好&安培;&安培; thisGood);
}


解决方案

  

我的理解是,默认的拷贝赋值运算符执行成员复制,这对于阵列成员(不指针到数组成员)的带来的数组的elementwise副本。


是的。这是正确的。

您的问题是不是与拷贝赋值运算符(除非你已经发现了一些不寻常的编译器缺陷,这是不可能的)。

I've got a class definition similar to the following:

class UUID
{
  public:
    // Using implicit copy assignment operator

  private:
    unsigned char buffer[16];
};

I've just had a unit test fail on me that was verifying that copy assignment worked properly. To my surprise, one byte in the middle of the buffer[] array was copied incorrectly.

My understanding is that the default copy assignment operator performs memberwise copy, and that for array members (not pointer-to-array members) that entails elementwise copy of the array. Am I mistaken?

My gut feeling here is that I've been bitten by a dangling pointer somewhere that has stomped on the middle of my array. But, I'm seeing this repeatably when, e.g. I copy a vector of these objects into another vector.

Anybody care to tell me where I've gone wrong?

Edit:

To expand on this a bit, the class is not a POD type--it derives from a few abstract base classes and thus has a virtual destructor. However, the array is the only data member, and the usage which broke in the unit test was this:

const int n = 100;

std::vector<UUID> src, dst;
src.reserve(n);
dst.resize(n);

for (int i = 0; i < n; ++i) {
  UUID id;
  src.push_back(id);
}

for (int i = 0; i < n; ++i) {
  dst[i] = src[i];
}

bool good = true;
for (int i = 0; i < n; ++i) {
  const bool thisGood = (dst[i] == src[i]);

  std::cout << "i = " << i << " -> src = '" << src[i]
            << "', dst = '" << dst[i] << "', src == dst ? "
            << thisGood << '\n';

  good = (good && thisGood);
}

解决方案

My understanding is that the default copy assignment operator performs memberwise copy, and that for array members (not pointer-to-array members) that entailed elementwise copy of the array.

Yes. This is correct.

Your problem is not with the copy assignment operator (unless you have found some unusual compiler bug, which is unlikely).

这篇关于与数组成员默认拷贝赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆