我对多个虚拟析构函数有了一些想法,特别是.阅读后阅读http://blogs.msdn.com/b/oldnewthing/archive/2004/05/07/127826.aspx.
假设我有
class Base
{
public:
Base();
virtual ~Base();
private:
Logger* _logger;
};
//and
class Derived : public Base{
public:
Derived();
virtual ~Derived();
private:
Logger* _logger;
};
Run Code Online (Sandbox Code Playgroud)
在cpp文件中,在每个析构函数中我删除了各自的_logger指针
Base::~Base(){ //base.cpp
delete _logger;
}
Derived::~Derived(){ //derived.cpp
delete _logger;
}
Run Code Online (Sandbox Code Playgroud)
这会像我预期的那样工作,没有内存泄漏吗?
假设我有一个自定义的多标准比较器,尽管多部分可能并不重要.为了简单起见,我们假设我们正在排序由3个表示坐标的双精度组成的数组.
我知道go-to比较运算符是"<",但我有这种唠叨的感觉,如果所有部分都相等,"<="可能会保存交换.分拣机(例如std :: sort)是否会,"嘿,如果比较器返回false,我正在交换你!",或者这是不正确的假设?谢谢.
// Compare based on X, then Y, then Z
bool PointComparer(const array<double,3>& a, const array<double,3>& b)
{
if (a[0] < b[0]) return true;
if (a[0] > b[0]) return false;
if (a[1] < b[1]) return true;
if (a[1] > b[1]) return false;
return a[2] < b[2]; // If instead was a[2] <= b[2], would it save a swap in equal case?
}
// Later sort a collection of these arrays
Run Code Online (Sandbox Code Playgroud)