使用数组复制struct [英] Copying struct with array

查看:84
本文介绍了使用数组复制struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个


struct S

{

int arr [5];

};


和我这样做:


S s1,s2;


....


s1 = s2;


会发生什么?


通常,数组任务是不可能的/允许的。如果我做s1.arr =

s2.arr我得到编译器错误。如果我做s1 = s2它编译得很好。并且它似乎是
甚至似乎是数组被复制,而不仅仅是指向数组的

起点,至少用g ++。

C和C ++之间的语义有什么不同吗?


/ David

解决方案

< blockquote>David Rasmussen <哒************* @ gmx.net>在消息中写道

news:40a0fafd


0


174

struct S
{arr [5];
};
s1 = s2;

会发生什么?

通常情况下,阵列分配是不可能/不允许的。如果我做s1.arr =
s2.arr我得到编译器错误。如果我做s1 = s2它编译得很好。它甚至看起来像数组被复制,而不仅仅是指向数组开始的指针,至少用g ++。

这里的语义是否有任何区别在C和C ++之间?




是的,赋值在C和C ++中复制数组内容。复制构造函数相同的



If I have this

struct S
{
int arr[5];
};

and I do this:

S s1,s2;

....

s1 = s2;

what happens?

Normally, array assignments aren''t possible/allowed. If I do s1.arr =
s2.arr I get a compiler error. If I do s1 = s2 it compiles fine. And it
even seems like the array is getting copied, not just the pointer to the
start of the array, at least with g++.

Is there any difference in semantics here betweeen C and C++?

/David

解决方案

"David Rasmussen" <da*************@gmx.net> wrote in message
news:40a0fafd


0


174

struct S
{
int arr[5];
}; s1 = s2;

what happens?

Normally, array assignments aren''t possible/allowed. If I do s1.arr =
s2.arr I get a compiler error. If I do s1 = s2 it compiles fine. And it
even seems like the array is getting copied, not just the pointer to the
start of the array, at least with g++.

Is there any difference in semantics here betweeen C and C++?



Yes, the assignment copies the array contents, in both C and C++. Same
thing for the copy constructor.


这篇关于使用数组复制struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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