Rvalue as an initializer for creating an object

I am new to programming. Sorry for my bad english. I tried using rvalue as an initializer for initial objects. Thus, in accordance with the code, it will print out what the constructor used and the assignment operator are. But it turned out the object "what2" and "what3", which does not print anything. here is the code:

#include <iostream> using namespace std; class X{ public: int x; X()= default; X(int num):x{num}{} X(const X& y){ x = yx; std::cout << "copy constructor" << std::endl; std::cout << x << std::endl; } X& operator=(const X& d){ x = dx; std::cout << "copy assignment" << std::endl; return *this; } X(X&& y){ x = yx; std::cout << "move constructor" << std::endl; } X& operator=(X&& b){ x = bx; std::cout << "move assignment" << std::endl; return *this; } }; X operator +(const X& a,const X& b){ X tmp{}; tmp.x = ax +bx; return tmp; } int main(int argc, const char * argv[]) { X a{7} , b{11}; X what1; cout << "---------------" << endl; what1 = a+b; cout << "---------------" << endl; X what2{a+b}; cout << "---------------" << endl; X what3 = a+b; cout << "---------------" << endl; std::cout << what1.x << std::endl; std::cout << what2.x << std:: endl; std::cout <<what3.x << std::endl; return 0; } 

output:

 --------------- move assignment --------------- --------------- --------------- 18 18 18 Program ended with exit code: 0 

only "what1" uses the assignment correctly. so how can i use rvalue to initialize an object? and using operator = to initialize the object? thank you very much.

+5
source share
3 answers

Your code may lead to the use of relocation operations, but your compiler selected elide these relocations and highlighted returning operator+ directly to the call site. You can see that this happens if you disable elision copying in your compiler ( -fno-elide-constructors in GCC or Clang).

Your move constructor and assignment operator will be used successfully in contexts in which a copy exception is not allowed, for example:

 X what2 { std::move(what1) }; //can't elide copy, move constructor used 
+4
source

GCC provides the -fno-elide-constructors to disable copying. if you want to exclude copying, use the -fno-elide-constructors flag. See https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization/27916892#27916892 for details

0
source

The following code runs more of your constructors / statements, check it to see which trigger is when

 #include <iostream> using namespace std; class X{ public: int x; X() { x = 0; std::cout << "constructor" << std::endl; } X(int num):x{num} { std::cout << "list initilizer" << std::endl; } X(const X& y){ x = yx; std::cout << "copy constructor" << std::endl; } X& operator=(const X& d){ x = dx; std::cout << "copy assignment" << std::endl; return *this; } X(X&& y){ x = yx; std::cout << "move constructor" << std::endl; } X& operator=(X&& b){ x = bx; std::cout << "move assignment" << std::endl; return *this; } }; X operator +(const X& a,const X& b){ X tmp{}; tmp.x = ax +bx; return tmp; } int main(int argc, const char * argv[]) { X a{7} , b{11}; cout << "---------------" << endl; X what1; cout << "---------------" << endl; what1 = a+b; cout << "---------------" << endl; X what2(a+b); cout << "---------------" << endl; X what3 = X(a); cout << "---------------" << endl; X what4 = X(); cout << "---------------" << endl; X what5 = std::move(what1); cout << "---------------" << endl; what5 = std::move(what1); cout << "---------------" << endl; what5 = what1; cout << "---------------" << endl; return 0; } 
0
source

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


All Articles