#include <iostream>
#include <string.h>
using namespace std;
class A
{
public:
A(char *ps)
{
name = new char[strlen(ps) +1];
len = strlen(ps);
memcpy(name, ps, len);
cout<<this<<" constructor A"<<endl;
}
A(const A& another)
{
len = strlen(another.name);
name = new char[len +1];
strcpy(name,another.name);
cout<<this<<" copy A"<<endl;
}
A & operator=(const A & another)
{
cout<<this<<" = A"<<endl;
if(this == &another)
return *this;
else
{
delete []this->name;
int lens = strlen(another.name);
this->name = new char[lens +1];
strcpy(this->name, another.name);
return *this;
}
}
~A()
{
cout<<this<<" destructor A"<<endl;
}
void dis()
{
cout<<len<<endl;
cout<<name<<endl;
}
private:
int len;
char *name;
};
int main()
{
cout << "len " << strlen("helloworld") << endl;
A a("helloworld");
a.dis();
A b(a);
b.dis();
A c = b;
c.dis();
A d("helloworld2");
d = c;
d.dis();
return 0;
}
5.C++构造 析构 拷贝 赋值函数
猜你喜欢
转载自blog.csdn.net/liuqingsongmsdn2014/article/details/108985968
今日推荐
周排行