Basically, the argument of the move constructor is the class itself.
However, if I want to construct an object of the class from a lvalue without copy operation can I do like this?
class A{
A(const LargeDataType& inData):data(inData) {} // constructor 1
A(LargeDataType&& inData):data(std::move(inData)) {} // constructor 2
private:
LargeDataType data;
};
To use it:
Method 1:
LargeDataType outData = 100;
A objA(std::move(outData)); // call constructor 2
Method 2 (If constructor 2 was not implemented):
LargeDataType outData = 100;
A objA(std::move(outData)); // call constructor 1
In this way, there is no copy operation when constructing the objA. My questions are:
Is this legal to create a move constructor like this?
This is more efficient than traditional constructor because no copy needed during objA creation?
Whether method 2 could be better and has the same efficiency as the method 1?
Thanks a lot!