类与对象R-2-1-1强制
禁止含动态分配成员的类省略拷贝构造函数和赋值操作符
未编写拷贝构造函数和赋值操作符
test.cpp
1class Foo禁止含动态分配成员的类省略拷贝构造函数和赋值操作符 [gjb8114-r-2-1-1]2{3 int *ptr;4public:5 Foo();6 ~Foo();7};89Foo::Foo() : ptr(new int(0)) {}1011Foo::~Foo() { delete ptr; }
已编写拷贝构造函数和赋值操作符
test.cpp
1class Foo2{3 int *ptr;4public:5 Foo();6 ~Foo();7 Foo(const Foo &other);8 Foo &operator=(const Foo &rhs);9};1011Foo::Foo() : ptr(new int(0)) {}1213Foo::~Foo() { delete ptr; }1415Foo::Foo(const Foo &other) : ptr(new int(*other.ptr)) {}1617Foo &Foo::operator=(const Foo &rhs)18{19 if (this != &rhs)20 *ptr = *rhs.ptr;21 return *this;22}