在这种情况下,如何正确实施 "operator=" 和 "copy c'tor"?
How can I implement "operator=" and "copy c'tor" correctly in this case?
鉴于 classes BaseClass
和 SomeClass
(实现 operator=
和 copy c'tor
),我写了以下 class:
class DerivedClass : public BaseClass {
SomeClass* a1;
SomeClass* a2;
public:
// constructors go here …
~DerivedClass() { delete a1; delete a2;}
// other functions go here ...
};
我的问题是: 我如何实现 class DerivedClass
的 operator=
?我如何实现这个 class 的 copy c'tor
?
我想通过以下方式实现 operator=
:
DerivedClass& operator=(const DerivedClass& d) {
if (this==&d) return *this;
SomeClass* tmp1 = new SomeClass (*(d.a1));
SomeClass* tmp2 = NULL;
try {
tmp2 = new SomeClass(*(d.a2));
} catch (const std::exception& e) {
delete tmp1;
throw e;
}
delete this->a1;
delete this->a2;
this->a1 = tmp1;
this->a2 = tmp2;
return *this;
}
但是我不确定解决方案,特别是BaseClass
的字段呢?
另外,如何实现DerivedClass
的copy c'tor
?我可以用 operator=
做吗?
我的建议是您不要实施它们,而是选择the rule of zero. That can be easily accomplished with e.g. std::shared_ptr
(如果 a1
和 a2
是可以接受的)。
但是如果您必须实现自己的复制构造函数和复制赋值运算符,那么您首先需要调用父 class 构造函数或运算符,然后再进行自己的复制。
对于构造函数,您使用构造函数初始化列表:
DerivedClass(DerivedClass const& other)
: BaseClass(other), a1(new SomeClass(*other.a1)), a2(new SomeClass(*other.a2))
{}
a1(new SomeClass(*other.a1)
部分使用其复制构造函数创建一个新的 SomeClass
对象,并初始化您的成员 a1
以指向新对象。
这最后一部分也应该是对您的复制赋值运算符的提示,因为它也可以在那里使用:
DerivedClass& operator=(DerivedClass const& other)
{
if (this != &other)
{
// First let the parent copy itself
BaseClass::operator=(other);
// Deep copy of the members
delete a1;
a1 = new SomeClass(*other.a1);
delete a2;
a2 = new SomeClass(*other.a2);
}
return *this;
}
鉴于 classes BaseClass
和 SomeClass
(实现 operator=
和 copy c'tor
),我写了以下 class:
class DerivedClass : public BaseClass {
SomeClass* a1;
SomeClass* a2;
public:
// constructors go here …
~DerivedClass() { delete a1; delete a2;}
// other functions go here ...
};
我的问题是: 我如何实现 class DerivedClass
的 operator=
?我如何实现这个 class 的 copy c'tor
?
我想通过以下方式实现 operator=
:
DerivedClass& operator=(const DerivedClass& d) {
if (this==&d) return *this;
SomeClass* tmp1 = new SomeClass (*(d.a1));
SomeClass* tmp2 = NULL;
try {
tmp2 = new SomeClass(*(d.a2));
} catch (const std::exception& e) {
delete tmp1;
throw e;
}
delete this->a1;
delete this->a2;
this->a1 = tmp1;
this->a2 = tmp2;
return *this;
}
但是我不确定解决方案,特别是BaseClass
的字段呢?
另外,如何实现DerivedClass
的copy c'tor
?我可以用 operator=
做吗?
我的建议是您不要实施它们,而是选择the rule of zero. That can be easily accomplished with e.g. std::shared_ptr
(如果 a1
和 a2
是可以接受的)。
但是如果您必须实现自己的复制构造函数和复制赋值运算符,那么您首先需要调用父 class 构造函数或运算符,然后再进行自己的复制。
对于构造函数,您使用构造函数初始化列表:
DerivedClass(DerivedClass const& other)
: BaseClass(other), a1(new SomeClass(*other.a1)), a2(new SomeClass(*other.a2))
{}
a1(new SomeClass(*other.a1)
部分使用其复制构造函数创建一个新的 SomeClass
对象,并初始化您的成员 a1
以指向新对象。
这最后一部分也应该是对您的复制赋值运算符的提示,因为它也可以在那里使用:
DerivedClass& operator=(DerivedClass const& other)
{
if (this != &other)
{
// First let the parent copy itself
BaseClass::operator=(other);
// Deep copy of the members
delete a1;
a1 = new SomeClass(*other.a1);
delete a2;
a2 = new SomeClass(*other.a2);
}
return *this;
}