我warning C4355: 'this' : used in base member initializer list
从 Visual C++ 2010 得到一个:
我有一个持有句柄的类,我想自动关闭句柄,即使该类的 ctor 失败(因此不调用它的 dtor)。但是,我不想费心制作一个完整的句柄包装类,而宁愿将它保存在一个智能指针中。所以我写了这个:
foo.h
~~~~~
class Foo
{
...
Log &_log;
std::unique_ptr<void, std::function<void (void *)>> _handle;
...
}
foo.cpp
~~~~~~~
#include <windows.h>
Foo::Foo(Log &lg, ...) : _log(lg), ... _handle(nullptr, [&](void *h){ if (h) { if (!CloseHandle(h)) LOG(_log, "Could not close port: " << LastWinErr()); h = nullptr; } })
{
HANDLE h(CreateFile( ...
if (h == ...
_handle.reset(h);
... // Bunch of other stuff that could potentially throw
}
在关闭之前,我用类似的东西初始化 _handle _handle(nullptr, bind(PortDeleter, placeholders::_1, ref(_log)))
,但这需要一个单独的定义。
我的问题:警告是否与此特定实例有关?无论哪种方式,详细原因是什么?有没有一种简单的方法来避免它?