浅拷贝与深拷贝

#include <iostream>
#include <string.h>
using namespace std;

class Person
{
public:
	Person(char *pN = "no name")
	{
		cout<<"Constructing "<<pN<<endl;
		pName = new char[strlen(pN) + 1];
		if (pName)
		{
			strcpy(pName,pN);
		}
	}

	Person(Person &p)
	{
		cout<<"Copying "<<p.pName<<" into its own block"<<endl;
		pName = new char[strlen(p.pName) + 1];
		if (pName)
		{
			strcpy(pName,p.pName);
		}
	}

	~Person()
	{
		cout<<"Destructing "<<pName<<endl;
		pName[0] = '\0';
		delete pName;
	}

	

protected:
	char *pName;
};


void main()
{
	Person p1("randy");
	Person p2 = p1;
}
posted @ 2010-10-13 19:11  Cranny  阅读(278)  评论(1编辑  收藏  举报