-1

私はムーブ コンストラクターの初心者です。いくつかのサイトから調査し、Visual Studio 11 Express Beta を使用してみました。

以下は私のテストコードです...

#include <iostream>

using namespace std;

class Foo
{
public:
    Foo()
      : Memory(nullptr)
    {
        cout<<"Foo Constructor"<<endl;
    }
    ~Foo()
    {
        cout<<"~Foo Destructor"<<endl;
        if(Memory != nullptr)
            delete []Memory;
    }
    Foo(Foo& rhs)
      : Memory(nullptr)
    {
        cout<<"Copy Constructor"<<endl;

        //allocate
        //this->Memory = new ....
        //copy 
        //memcpy(this->Memory, rhs.Memory...);
    }

    Foo& operator=(Foo& rhs)
    {
        cout<<"="<<endl;
    }
    void* Memory;

    Foo(int nBytes) { Memory = new char[nBytes]; }

    Foo(Foo&& rhs)
    {
        cout<<"Foo Move Constructor"<<endl;
        Memory = rhs.Memory;
        rhs.Memory = nullptr;
    }
};

Foo Get()
{
    Foo f;
    return f; 
    //return Foo();
}
void Set(Foo rhs)
{
    Foo obj(rhs);
}
int main()
{
    Set(Get());
    return 0;
}

移動コンストラクタに入らない理由がわかりません。

これは実際には Get(); の Rvalue です。

const コンストラクターから非 const コピー コンストラクターを変更した場合、

移動コンストラクターに入ります。挙動が変わった…

なぜそれが起こったのか、誰か親切に説明してもらえますか?

4

1 に答える 1

1
#include <iostream>

using namespace std;

class Foo
{
public:
    Foo():
        Memory(nullptr)
    {
        cout<< this << "Foo Constructor"<<endl;
    }

    ~Foo()
    {
        cout<< this << "~Foo Destructor"<<endl;
        if(Memory != nullptr)
            delete []Memory;
    }

    Foo(Foo& rhs)
        :Memory(nullptr)
    {
        cout<<this << "Copy Constructor"<<endl;

        //allocate
        //this->Memory = new ....
        //copy 
        //memcpy(this->Memory, rhs.Memory...);
    }

    Foo& operator=(Foo& rhs)
    {
        cout<<"="<<endl;
    }
    void* Memory;

    Foo(int nBytes) { Memory = new char[nBytes]; }

    Foo(Foo&& rhs)
        {
        cout<<this << "Foo Move Constructor"<<endl;

                 Memory = rhs.Memory;


                 rhs.Memory = nullptr;
        }

};

Foo Get()
{
    Foo f;
    cout << &f << "f" <<endl;
    return f; 
}

void Set(Foo rhs)
{
    Foo obj(rhs);
    cout << &obj << "obj"<<endl;
}

int main()
{
    Set(Get());
    return 0;
}

出力...

0x7fffe38fa0a0 Foo Constructor
0x7fffe38fa0a0 f
0x7fffe38fa070 Copy Constructor
0x7fffe38fa070 obj
0x7fffe38fa070 ~Foo Destructor
0x7fffe38fa0a0 ~Foo Destructor

回答:名前付き戻り値の最適化により、パラメーター rhs はローカル変数 f のエイリアスとしてインプレースで構築されます。(つまり、 rhs と f は同じインスタンスです)。

rhs は左辺値であるため、コピー コンストラクターを使用して、rhs から構成 obj をコピーします。

于 2012-03-14T09:55:28.730 に答える