//浅拷贝
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=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();
return 0;
}
C++浅拷贝
最新推荐文章于 2024-08-15 05:02:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)