<分区>
Possible Duplicate:
What is The Rule of Three?
人们说,如果你需要一个析构函数,那么你实际上需要一个重载的 operator=
struct longlife{ };
class z
{
z(){};
~z(){ for( auto it=hold.begin();it!=hold.end() ++it ) delete(*it); };
vector<longlife*> hold;
};
假设在hold
上插入的所有指针都是新
堆分配的,为什么在这个例子中除了解构函数还需要其他东西?
任何其他
我的意思是,
z& operator=( const z&ref )
{
hold = ref.hold;
return *this;
}
会:
z a;
a.hold.push_back( heap_item );
z a2;
a2 = a;
导致内存泄漏?有时很难理解为什么三的规则是规则