我对多个虚拟析构函数有了一些重新考虑,尤其是。在阅读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;
};
在 cpp 文件中,在每个析构函数中,我正在删除相应的_logger
指针
Base::~Base(){ //base.cpp
delete _logger;
}
Derived::~Derived(){ //derived.cpp
delete _logger;
}
这会按我的预期工作,没有内存泄漏吗?