1
#include <iostream>

using std::cout;
using std::endl;

class MoveTest {
public:
    MoveTest(int i) :
            _i(i) {
        cout << "Normal Constructor" << endl;
    }
    MoveTest(const MoveTest& other) :
            _i(other._i) {
        cout << "Copy Constructor" << endl;
    } // = delete;
    MoveTest& operator=(const MoveTest& other) {
        return *this;
    } //= delete;
    MoveTest(MoveTest&& o) {
        _i = o._i;
        cout << "Move Constructor" << endl;
    }
    MoveTest& operator=(const MoveTest&& o) {
        cout << "Move Assign" << endl;
        return *this;
    }
//private:
    int _i;
};

MoveTest get() {
    MoveTest t = MoveTest(2);
    cout << "get() construct" << endl;
    return t; //MoveTest(1);
}

int main(int argc, char **argv) {
    MoveTest t(get());
    cout << t._i << endl;
    t = get();
}

There is result:

Normal Constructor

get() construct

2

Normal Constructor

get() construct

Move Assign

In main function,'MoveTest t(get());' neither use a copy construct nor use a move construct.This is why?

0 Answers0