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.
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)); }
Source: https://habr.com/ru/post/1389420/More articles:Python string module vs str - pythonNHibernate / Castle.ActiveRecord; Session management; WinForms - winformsRedis Internals - LRU sample implementation - redishttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1389418/add-css-to-head-from-template-file-in-magento&usg=ALkJrhhQOGb6_M5E8QkgDtwAl099rs9DIATFS build server takes longer to build than local machine - msbuildJquery dialog box return value - javascriptellipse ice cream sandwich adds [- androidEllipsize attribute not working on Android 4.0 - androidCan PHP move and edit root system files on a server? - securityHow to break a string with special characters on NSMutableArray - iosAll Articles