fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A{
  5. public:
  6. int a;
  7.  
  8. A(int i){a = i;}
  9.  
  10. A(const A& rhs) // copy constructor
  11. {
  12. a = 2;
  13. }
  14.  
  15. A(const A&& rhs) // move constructor
  16. {
  17. a = 3;
  18. }
  19.  
  20. };
  21.  
  22. void print(A a)
  23. {
  24. cout << a.a << endl;
  25. }
  26.  
  27. A createA()
  28. {
  29. A a(1);
  30. if( false )
  31. return A(1);
  32. else
  33. return a;
  34. }
  35.  
  36. int main(){
  37.  
  38. A a = createA();
  39. print(a);// This will invoker copy constructor and output is 2
  40.  
  41. print(createA()); // This should invoke move constructor because this object here is rvalue right?
  42. }
  43.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
2
3