C++深拷贝和浅拷贝构造函数

深拷贝:内存拷贝,每块内存有不同的指针

浅拷贝:指针的拷贝,拷贝之后指向同一块内存

 

typedef struct {
	int index;			//对应APP接口协议中的索引号
	int iRegThrdCnt;	//注册的线程个数
}TMsgGather;

class CMsgGather
{
public:
	CMsgGather();
	~CMsgGather();

	CMsgGather(const CMsgGather &msgGather); //深拷贝构造

private:
	TMsgGather msgGather[128];
};

CMsgGather::CMsgGather()
{
	printf("CMsgGather::CMsgGather\n");
}

CMsgGather::~CMsgGather()
{
	printf("CMsgGather::~CMsgGather\n");
}

CMsgGather::CMsgGather(const CMsgGather &msgGather)
{
	printf("CMsgGather::CMsgGather(const CMsgGather &msgGather)\n");
}

 

#include "MsgGather.h"

void test(CMsgGather msgGather)
{
	printf("test(CMsgGather msgGather)\n");
}

int main(int argc, char *argv[])
{
	try
	{
		CMsgGather msgGather;
		test(msgGather);
	}
	catch (const std::exception&)
	{

	}
}

效果:

 

おすすめ

転載: blog.csdn.net/chenliang0224/article/details/112506226