gpt4 book ai didi

c++ - 如何在复制控制函数中处理 C++ 数组成员?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:56:11 25 4
gpt4 key购买 nike

这是我想了很久的事情。举个例子:

struct matrix
{
float data[16];
};

我知道默认构造函数和析构函数在这个特定示例中做了什么(什么都不知道),但是复制构造函数和复制赋值运算符呢?

struct matrix
{
float data[16];

// automatically generated copy constructor
matrix(const matrix& that) : // What happens here?
{
// (or here?)
}

// automatically generated copy assignment operator
matrix& operator=(const matrix& that)
{
// What happens here?

return *this;
}
};

它是否涉及 std::copystd::uninitialized_copymemcpymemmove 或什么?

最佳答案

这是标准在 12.8(复制类对象)中所说的。复制构造:

Each subobject is copied in the manner appropriate to its type:

  • if the subobject is of class type, the copy constructor for the class is used;
  • if the subobject is an array, each element is copied, in the manner appropriate to the element type;
  • if the subobject is of scalar type, the built-in assignment operator is used.

复制作业:

Each subobject is assigned in the manner appropriate to its type:

  • if the subobject is of class type, the copy assignment operator for the class is used (as if by explicit qualification; that is, ignoring any possible virtual overriding functions in more derived classes);
  • if the subobject is an array, each element is assigned, in the manner appropriate to the element type;
  • if the subobject is of scalar type, the built-in assignment operator is used.

关于c++ - 如何在复制控制函数中处理 C++ 数组成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18517566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com