C++ 移动构造函数调用基类移动构造函数
我有一个基类,基本上是将类附加到任意windows句柄(例如,HWND、HFONT),并使用策略类附加/分离和销毁:C++ 移动构造函数调用基类移动构造函数,c++,templates,c++11,move-constructor,C++,Templates,C++11,Move Constructor,我有一个基类,基本上是将类附加到任意windows句柄(例如,HWND、HFONT),并使用策略类附加/分离和销毁: // class SmartHandle template<typename THANDLE, class TWRAPPER, class TPOLICY> class SmartHandle : boost::noncopyable { private: TPOLICY* m_pPolicy; // Policy bool m_bIsTemp
// 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
再次,复制默认/复制系数。移动构造函数的实现是:
Window::Window(Window&& _rhs) : SmartHandle(_rhs)
{
}; // eo mtor
但是,在编译过程中,我在move构造函数实现的第一行上遇到以下错误:
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>'
1>c:\\documents\visual studio 2010\projects\gallow\gallow\window.cpp(81):错误C2248:“gallow::SmartHandle::SmartHandle”:无法访问在类“gallow::SmartHandle”中声明的私有成员
因此,它似乎试图调用复制构造函数(我已声明为private),而不是移动构造函数。这里有没有我遗漏的简单的东西
提前谢谢
编辑:修改了mtor,使其为非常量,错误仍然存在。
Edt2:我使用Visual C++ 2010。< /P> < P>一个移动构造函数应该是代码> t(t&&)/COD>,不是<代码> T(const t&&)。<命名>参数不被视为rValk引用,您必须“<代码>移动< /代码>它。
Window::Window(Window&& _rhs) : SmartHandle(std::move(_rhs))
{
}
参数不被视为右值的原因是它可以使用两次,移动通常会更改值,因此必须明确知道此变量是从何处移动的
e、 g
void f(字符串和第一个、字符串和第二个)
{
字符串local=first;
实际上应该是这样的
Window::Window(Window&& _rhs) : SmartHandle( std::forward<SmartHandle>( _rhs ) ) { }; // eo mtor
Window::Window(Window&&u-rhs):智能句柄(std::forward(_-rhs)){};//eo-mtor
谢谢,我修改了这个,但遗憾的是,它与我收到的错误无关。重复?埃里克,谢谢,我没有听清楚。是的,这完全解决了问题,希望我能接受你的回答:)
void f(string&& first, string&& second)
{
string local = first;
cout << first; // I would be surprised if this is different from `local`
local = std::move(second);
// If I'm surprised after explicitly moving from second it's my problem
}
Window::Window(Window&& _rhs) : SmartHandle( std::forward<SmartHandle>( _rhs ) ) { }; // eo mtor