使用数组复制struct [英] Copying struct with array
问题描述
如果我有这个
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屋!