15

クラスを任意のウィンドウ ハンドル (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

クラスをコピーしたくないので、コピー コンストラクターをプライベート (実装なし) と宣言しましたが、移動許可されていることに注意してください。

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

もう一度、default/copy ctors をコピーします。移動コンストラクターの実装は次のとおりです。

    Window::Window(Window&& _rhs) : SmartHandle(_rhs)
    {
    };  // eo mtor

ただし、コンパイル中に移動コンストラクター実装の最初の行で次のエラーが発生します。

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>'

そのため、移動コンストラクターではなく、コピー コンストラクター (プライベートと宣言しました) を呼び出そうとしているように見えます。ここに欠けている簡単なものはありますか?

前もって感謝します。

編集: mtor を非 const に変更したため、エラーが残ります。EDIT2: Visual C++ 2010 を使用しています。

4

3 に答える 3

13

実際にはこのようになっているはずです。

Window::Window(Window&& _rhs) : SmartHandle( std::forward<SmartHandle>( _rhs ) )     {     };  // eo mtor 

http://msdn.microsoft.com/en-us/library/ee390914.aspx

于 2011-02-01T18:43:32.947 に答える
10

名前付き引数は、必要な右辺値参照としては扱われませんmove

Window::Window(Window&& _rhs) : SmartHandle(std::move(_rhs))
{
}

引数が右辺値として扱われない理由は、引数を2回使用でき、通常、移動すると値が変更されるため、この変数が移動元であることを明示する必要があるためです。

例えば

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
}

a)明確であり、b)冗長でエラーが発生しやすいタイプを指定する必要があるため、移動する場合moveよりも使用することをお勧めします。forwardforward

于 2010-12-12T09:36:10.827 に答える
5

ムーブ コンストラクターはT(T&&)ではなく、 である必要がありT(const T&&)ます。

于 2010-12-08T18:30:53.047 に答える