fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Test {
  6. public:
  7. Test() { cout << "Test()\n"; }
  8. Test(int x):val_(x){ cout << "Test(" << x << ")\n"; }
  9. Test(const Test& t):val_(t.val_) { cout << "Test(const Test& " << t.val_ << ")\n"; }
  10. Test(Test&&t) :val_(t.val_) { t.val_ = 0; cout << "Test(const Test&& " << t.val_ << ")\n"; }
  11. Test& operator = (const Test& t) {
  12. cout << "Test& operator = (const Test& " << t.val_ <<")\n";
  13. val_ = t.val_;
  14. return *this;}
  15. Test& operator = (Test&& t) {
  16. cout << "Test& operator = (const Test&&" << t.val_ <<")\n";
  17. val_ = t.val_; t.val_ = 0;
  18. return *this;}
  19. ~Test() { cout << "~Test(" << val_ <<")\n"; }
  20. int val() const { return val_; }
  21. private:
  22. int val_ = 0;
  23. };
  24.  
  25.  
  26. int main(int argc, const char * argv[])
  27. {
  28. long long L, M;
  29. Test * t = reinterpret_cast<Test*>(&L);
  30. *t = Test(5);
  31. cout << "t->val() = " << t->val() << endl;
  32. cout << "-------\n";
  33.  
  34. Test * u = new(&M) Test(6);
  35. cout << "u->val() = " << u->val() << endl;
  36.  
  37. }
  38.  
  39.  
Success #stdin #stdout 0s 4400KB
stdin
Standard input is empty
stdout
Test(5)
Test& operator = (const Test&&5)
~Test(0)
t->val() = 5
-------
Test(6)
u->val() = 6