关于网友提出的“ 结构可以整体复制吗?”问题疑问,本网通过在网上对“ 结构可以整体复制吗?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 结构可以整体复制吗?描述:
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));
正确,原因是:
在C++中,struct 与 class 的唯一区别就是 struct默认成员是public,而class默认是private,因此,struct也象class一样有默认的copy constructor,即内存直接拷贝的copy constructor。同时,你的struct 这样赋值时应该注意到 “深拷贝”问题
b=a不正确,因为这个struct中有个BYTE数组。