问题描述
我有一个基本类,它基本上将一个类附加到一个任意的窗口句柄(例如,HWND,HFONT),并使用策略类附加/分离和销毁:
I have a base class that basically wraps up attaching a class to a arbitrary windows handle (e.g, HWND, HFONT), and uses a policy class to attach/detach and destroy:
// class SmartHandle
template<typename THANDLE, class TWRAPPER, class TPOLICY>
class SmartHandle : boost::noncopyable
{
private:
TPOLICY* m_pPolicy; // Policy
bool m_bIsTemporary; // Is this a temporary window?
SmartHandle(); // no default ctor
SmartHandle(const SmartHandle<THANDLE, TWRAPPER, TPOLICY>&); // no cctor
protected:
THANDLE m_hHandle; // Handle to the underlying window
TPOLICY& policy() {return(*m_pPolicy);};
// ctor that attaches but is temporary
SmartHandle(const THANDLE& _handle, bool _temporary) : m_hHandle(_handle)
, m_bIsTemporary(_temporary)
{
m_pPolicy = new TPOLICY(reinterpret_cast<TWRAPPER&>(*this));
if(_handle)
m_pPolicy->attach(_handle);
}; // eo ctor
// move ctor
SmartHandle(SmartHandle<THANDLE, TWRAPPER, TPOLICY>&& _rhs) : m_hHandle(_rhs.m_hHandle)
, m_bIsTemporary(_rhs.m_bIsTemporary)
{
m_pPolicy = new TPOLICY(reinterpret_cast<TWRAPPER&>(*this));
m_pPolicy->attach(m_hHandle);
const_cast<SmartHandle&>(_rhs).m_hHandle = NULL;
}; // eo mtor
// dtor
virtual ~SmartHandle()
{
if(m_hHandle)
{
m_pPolicy->detach(m_hHandle);
if(!m_bIsTemporary)
m_pPolicy->destroy(m_hHandle);
m_hHandle = NULL;
};
delete(m_pPolicy);
m_pPolicy = NULL;
}; // eo dtor
注意,我已经将复制构造函数私有
Note that I've declared the copy constructor private (with no implementation) as I do not want the class to be copied, but a move is allowed.
我的窗口
class派生自:
My Window
class derives from this:
class GALLOW_API Window : SmartHandle<HWND, Window, detail::_hWndPolicy>
{
friend class Application;
private:
static LRESULT CALLBACK wndProc(HWND _hWnd, UINT _message, WPARAM _wParam, LPARAM _lParam);
// no copy/default ctor
Window();
Window(const Window&);
protected:
public:
static const String ClassName;
Window(const HWND& _hWnd);
Window(const WindowCreateInfo& _createInfo);
Window(Window&& _rhs);
virtual ~Window();
}; // eo class Window
再次复制default / copy ctors。移动构造函数的实现是:
Once again, copy default/copy ctors. The implementation of the move constructor is:
Window::Window(Window&& _rhs) : SmartHandle(_rhs)
{
}; // eo mtor
但是,在编译期间,我在move构造函数的第一行获得以下错误实施:
However, during compilation I get the following error on the first line of the move constructor implementation:
1>c:\\documents\visual studio 2010\projects\gallow\gallow\window.cpp(81): error C2248: 'gallow::SmartHandle<THANDLE,TWRAPPER,TPOLICY>::SmartHandle' : cannot access private member declared in class 'gallow::SmartHandle<THANDLE,TWRAPPER,TPOLICY>'
所以,它似乎是试图调用复制构造函数比移动构造函数。有没有简单的我在这里缺少?
So, it appears as if it is trying to call the copy constructor (which I've declared private) rather than the move constructor. Is there something simple I am missing here?
提前感谢。
编辑:修改mtor非常数,错误仍然存在。
EDIT2:我使用Visual C ++ 2010。
Modified mtor so it was non-const, error remains. I am using Visual C++ 2010.
推荐答案
实际上应该是这样的。 b
$ b
actually it should be like this.
Window::Window(Window&& _rhs) : SmartHandle( std::forward<SmartHandle>( _rhs ) ) { }; // eo mtor
这篇关于移动构造函数调用基类Move构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!