기본적으로 임의의 창 핸들 (예 : HWND, HFONT)에 클래스를 연결하는 기본 클래스를 가지고 있으며 정책 클래스를 사용하여 첨부/분리 및 삭제합니다.이동 생성자 기본 클래스 이동 생성자
// 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
클래스를 복사하지 않으려 고 복사 생성자를 private (구현 없음)로 선언했지만, 은이 허용됩니다.
내 Window
클래스는에서 파생 :
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
다시 한번, 복사 기본/복사 ctors을. 이동 생성자의 구현은 다음과 같습니다
Window::Window(Window&& _rhs) : SmartHandle(_rhs)
{
}; // eo mtor
그러나, 컴파일시 I 이동 생성자 구현의 첫 번째 줄에 다음과 같은 오류 얻을 : 그것은 시도 것처럼 그래서
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>'
, 그것은 나타납니다 이동 생성자보다는 복사 생성자 (private로 선언 한)를 호출해야합니다. 여기 내가 누락 된 간단한 것이 있습니까?
미리 감사드립니다.
EDIT : 수정되지 않은 mtor이므로 오류가 유지됩니다. EDIT2 : Visual C++ 2010을 사용하고 있습니다.
중복? http://stackoverflow.com/questions/4086800/move-constructor-on-derived-object –
에릭, 고마워, 나는 그것을 잡지 못했다. 그래, 그 문제를 완전히 해결, 내가 당신의 대답을 받아 들일 수 있으면 좋겠어 :) –