类的正确副本和结构内容? [英] Right copy of classes ans struct contents?

查看:78
本文介绍了类的正确副本和结构内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了以下代码来复制struct和classes内容。结果在VS C ++中没问题,但我不知道它是否在linux中是正确的。

有程序员说浅拷贝和需要使用strdup,请看这里paxdiablo的第4个回复:

http://stackoverflow.com/questions/4931123/copying-one-structure-to-another



但是我使用VS2013对此进行了测试并没有看到任何影子副本:



我尝试过:



I made the following code to copy struct and classes contents. The result is ok in VS C++ but i do not know if it is right in linux.
There is programmers that said about "shallow copy" and need of use of strdup, see here the 4th response of "paxdiablo":
http://stackoverflow.com/questions/4931123/copying-one-structure-to-another

but I tested this using VS2013 and not saw any "shadow copy":

What I have tried:

#include <iostream>


struct s_struct
{
	int a, b;
	double w;
}s1,s2,s3,s4;

class c_class
{
public:
	int a, b;
	double w;
}c1,c2,c3;


using namespace std;


void main()
{
	s1.a = 10; s1.b = 12; s1.w = 100.111;
	memcpy(&s2, &s1, sizeof(s_struct));
	s3 = s1;
	if ((&s1 == &s3) || (&s1==&s2))
		cout << "STUPID copy of structures" << endl;
	else
		cout << "right copy of structures" << endl;
	c1.a = 22; c1.b = 24; c1.w = 200.222;
	memcpy(&c2, &c1, sizeof(c_class));
	c3 = c1;
	if ((&c1 == &c3) || (&c1 == &c2))
		cout << "STUPID copy of classes" << endl;
	else
		cout << "right copy of classes" << endl;


	cout << "=================== END ======================" << endl << endl;
	getchar(); getchar();
}

推荐答案

就目前而言这是可以的,但如果结构或类都不行包含指向其他对象的指针。您应该按照复制构造函数和复制赋值运算符(C ++) [<}中所述创建正确的复制构造函数。 a href =https://msdn.microsoft.com/en-us/library/87by589c.aspxtarget =_ blanktitle =New Window> ^ ]。



另请注意,这与Windows或Linux无关,这纯粹是C ++的一个问题。
That is OK as far as it goes, but it would not work if either the struct or the class contained pointers to other objects. You should create proper copy constructors as described at Copy Constructors and Copy Assignment Operators (C++)[^].

Note also this has nothing to do with Windows or Linux, it is purely an issue for C++.


只需复制内存即可。类或结构只包含基本成员( is_fundamental - C ++ Reference [ ^ ])如你的例子所示。



请注意,通常有一个隐式声明的复制赋值运算符,它还处理类成员(调用它们的复制运算符)。请参阅复制赋值运算符 - cppreference.com [ ^ ]。



如果你的类分配存储指针成员变量的内存,你应该总是提供一个复制操作符(如上面提到的SO链接 strdup 用于复制已分配字符串的内容。



Windows和Linux没有区别。它只是C ++。
It is OK to just copy the memory when your class or struct contains only fundamental members (is_fundamental - C++ Reference[^]) as in your example.

Note that there is usually an implicitly-declared copy assignment operator which handles also class members (calls their copy operator). See Copy assignment operator - cppreference.com[^].

If your class allocates memory storing the pointer in a member variable, you should always provide a copy operator (as in the mentioned SO link where strdup is used to copy the content of an allocated string).

There is no difference for Windows and Linux. It's just C++.


这篇关于类的正确副本和结构内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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