使用数组复制struct

编程入门 行业动态 更新时间:2024-10-13 14:24:20
本文介绍了使用数组复制struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我有这个 struct S { int arr [5]; }; 和我这样做: S s1,s2; .... s1 = s2; 会发生什么? 通常,数组任务是不可能的/允许的。如果我做s1.arr = s2.arr我得到编译器错误。如果我做s1 = s2它编译得很好。并且它似乎是甚至似乎是数组被复制,而不仅仅是指向数组的 起点,至少用g ++。 C和C ++之间的语义有什么不同吗? / David

解决方案

David Rasmussen <哒************* @ gmx>在消息中写道 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> 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

本文发布于:2023-10-08 14:51:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!