gpt4 book ai didi

c++ - 复制 C 风格的数组和结构

转载 作者:行者123 更新时间:2023-12-01 14:02:57 27 4
gpt4 key购买 nike

C++ 不允许使用 = 复制 C 风格的数组。但允许使用 = 复制结构,如此链接 -> Copying array in C v/s copying structure in C 。它还没有任何可靠的答案。
但请考虑以下代码

#include <iostream>
using namespace std;

struct user {
int a[4];
char c;
};

int main() {
user a{{1,2,3,4}, 'a'}, b{{4,5,6,7}, 'b'};
a = b; //should have given any error or warning but nothing
return 0;
}

上面的代码段没有给出任何错误和警告,而且工作正常。为什么?
考虑解释两个问题(这个和上面链接的一个)。

最佳答案

您的类user获得了implicitly declared copy constructorimplicitly declared copy assignment operator

隐式声明的复制赋值运算符将内容从 b 复制到 a

标准中的两段似乎适用:

class.copy.ctor

if the member is an array, each element is direct-initialized with the corresponding subobject of x;



class.copy.assign

if the subobject is an array, each element is assigned, in the manner appropriate to the element type;

关于c++ - 复制 C 风格的数组和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62167497/

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