0
这是我的母语的翻译。
你有一个类:我的解决方案有什么问题?
class Boo : public SuperBoo {
Foo* fFoo1;
Foo* fFoo2;
}
其中foo - 单形类和嘘声拥有指针fFoo1,fFoo2。
Boo的重载赋值运算符。
我的解决办法是:
class Foo
{
public:
Foo()
{}
};
class SuperBoo
{
public:
virtual ~SuperBoo()
{}
};
class Boo : public SuperBoo
{
public:
Boo(const int f1_id, const int f2_id)
{
f1 = new Foo(f1_id);
f2 = new Foo(f2_id);
}
~Boo()
{
delete f1;
delete f2;
}
/* C++11 only
Boo(Boo&& other)
{
std::swap(*this, other);
}
*/
Boo(const Boo& other)
{
f1 = new Foo(*(other.f1));
f2 = new Foo(*(other.f2));
}
Boo& operator=(Boo other)
{
std::swap(f1, other.f1);
std::swap(f2, other.f2);
return *this;
}
private:
Foo* f1;
Foo* f2;
};
但雇主不喜欢它。这里有什么问题?感谢帮助。
首先,它并不是特例安全。 – jrok 2013-03-15 11:06:14
同意,我将失去指针,如果新投掷 – 2013-03-15 11:09:14
有一个很好的解决这个异常安全问题的方法:使用内建的[智能指针类]之一(http://en.wikipedia.org/wiki/Smart_pointer# C.2B.2B_smart_pointers),它们使用RAII语言为您解决内存泄漏问题。 – 2013-03-15 11:14:14