C++ 数组成员如何在复制控制函数中处理? [英] How are C++ array members handled in copy control functions?

查看:20
本文介绍了C++ 数组成员如何在复制控制函数中处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is something I have wondered for a long time. Take the following example:

struct matrix
{
    float data[16];
};

我知道这个特定示例中默认构造函数和析构函数的作用(什么都没有),但是复制构造函数和复制赋值运算符呢?

I know what the default constructor and destructor do in this specific example (nothing), but what about the copy constructor and the copy assignment operator?

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 或什么?

Does it involve std::copy or std::uninitialized_copy or memcpy or memmove or what?

推荐答案

这是标准在 12.8 (Copying class objects) 中所说的.复制构造:

This is what the standard says in 12.8 (Copying class objects). Copy construction:

以适合其类型的方式复制每个子对象:

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

  • 如果子对象是类类型,则使用类的复制构造函数;
  • 如果子对象是数组,则以适合元素类型的方式复制每个元素;
  • 如果子对象是标量类型,则使用内置赋值运算符.

复制作业:

以适合其类型的方式分配每个子对象:

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

  • 如果子对象是类类型,则使用该类的复制赋值运算符(就像通过显式限定一样;也就是说,忽略更多派生类中任何可能的虚拟覆盖函数);
  • 如果子对象是数组,则以适合元素类型的方式分配每个元素;
  • 如果子对象是标量类型,则使用内置赋值运算符.

这篇关于C++ 数组成员如何在复制控制函数中处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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