今天突然被搞蒙,重新复习了一下
多说无益,直接上代码
#include <iostream>
#include <string.h>
using namespace std;
class A
{
public:
char * x;
int y;
A()
{
cout<<"无参构造"<<endl;
this->x=new char[0];
this->y=0;
}
A(int n)
{
cout<<"带参构造"<<endl;
this->y=n;
this->x=new char[n];
}
A(const A & a)
{
cout<<"拷贝构造"<<endl;
this->x=new char[strlen(a.x)+1];
strcpy(this->x,a.x);
this->y=a.y;
}
A & operator=(const A & a)
{
cout<<"重载的赋值运算符"<<endl;
if(this!=&a) //判断是否是自赋值
{
delete [] this->x; //释放
this->x=new char[strlen(a.x)+1]; //创建
strcpy(this->x,a.x);
this->y=a.y;
}
return *this;
}
~A()
{
delete [] x;
cout<<"析构"<<endl;
}
};
void func01(A aa)
{
}
A func02()
{
A aaa;
return aaa;
}
int main() {
A a(1);
A c = a;
func01(a);
c=func02();
c=a;
return 0;
}