How to call a move constructor?

The code below shows how to assign rvalue to object A in main?

#include <iostream> using namespace std; class A { public: int* x; A(int arg) : x(new int(arg)) { cout << "ctor" << endl;} A(const A& RVal) { x = new int(*RVal.x); cout << "copy ctor" << endl; } A(A&& RVal) { this->x = new int(*RVal.x); cout << "move ctor" << endl; } ~A() { delete x; } }; int main() { A a(8); A b = a; A&& c = A(4); // it does not call move ctor? why? cin.ignore(); return 0; } 

Thanks.

+4
source share
1 answer

Any named instance is an l-value.

Sample code with a move constructor:

 void foo(A&& value) { A b(std::move(value)); //move ctr } int main() { A c(5); // ctor A cc(std::move(c)); // move ctor foo(A(4)); } 
+7
source

Source: https://habr.com/ru/post/1389420/


All Articles