//深拷贝
class Ninja
{
int* m_pAge;
public:
Ninja()
{
cout<<"默认构造"<<endl;
}
explicit Nina(int age):m_pAge(NULL)//or m_pAge(new int(age))
{
if(age>=0)
{
m_pAge=new int (age);
}
cout<<"单参构造"<<endl;
}
Ninja(const Ninja& that)//深拷贝
{
m_pAge=new int;
*m_pAge=*(that.m_pAge);
}
~Ninja()
{
if(m_pAge)
{
delete m_pAge;
m_pAge=NULL;
}
cout<<"析构函数"<<endl;
}
void Introduce()const
{
cout<<"我今年"<<m_page<<"岁"<<endl;
}
}
int main()
{
Ninja* kakaxi=new Ninja(20)
kakaxi->Introduce();
Ninja* n1=new Ninjia(*kakaxi)
n1->Intdroduce();
delete kakaxi;
n1->Introduce();
delete n1;
return 0;
}
C++深拷贝
最新推荐文章于 2024-09-12 23:51:52 发布