结构可以整体复制吗?

来源:互联网  时间:2016/7/17 11:06:20

关于网友提出的“ 结构可以整体复制吗?”问题疑问,本网通过在网上对“ 结构可以整体复制吗?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 结构可以整体复制吗?
描述:

typedef struct{
int nlength;
BYTE data[102400];
}QElemType;
QElemType a,b;
a.nlenhth=102400;
memcpy(a.data,"hello",a.nlength);
b=a;//对吗?


解决方案1:

b=a;//正确
结构的付值实际上是内存的拷贝。
类似:
memcpy(&b,&a,sizeof(b));

解决方案2:

正确,原因是:
  在C++中,struct 与 class 的唯一区别就是 struct默认成员是public,而class默认是private,因此,struct也象class一样有默认的copy constructor,即内存直接拷贝的copy constructor。同时,你的struct 这样赋值时应该注意到 “深拷贝”问题

解决方案3:

b=a不正确,因为这个struct中有个BYTE数组。

上一篇ListCtrl中某ITEM的字体变为 加横线,怎么做?
下一篇怎样加载JPG图像资源在窗口中
明星图片
相关文章
《 结构可以整体复制吗?》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)