fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Foo
  4. {
  5. public:
  6. int a,b,c;
  7. double d,e,f;
  8. string text;
  9. Foo ()
  10. {
  11. cout << "Sup" << endl;
  12. }
  13. ~Foo()
  14. {
  15. cout << "R.I.P." << endl;
  16. }
  17.  
  18. static Foo getInstance()
  19. {
  20. Foo o;
  21. o.a = 42;
  22. o.text = "42";
  23. return o;
  24. }
  25. static Foo getInstanceWithoutNRVO()
  26. {
  27. if (false)
  28. return Foo();
  29.  
  30. Foo o;
  31. o.a = 43;
  32. o.text = "43";
  33. return o;
  34. }
  35. };
  36.  
  37. int main() {
  38. cout << "Creating \"a\"" << endl;
  39. Foo a;
  40.  
  41. cout << "Assigning a new value to \"a\"" << endl;
  42. a = Foo::getInstance();
  43. cout << a.a << " " << a.text << endl;
  44.  
  45. cout << "Assigning a new value to \"a\" without NRVO" << endl;
  46. a = Foo::getInstanceWithoutNRVO();
  47. cout << a.a << " " << a.text << endl;
  48.  
  49. cout << "Cleaning up and finishing work" << endl;
  50. return 0;
  51. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Creating "a"
Sup
Assigning a new value to "a"
Sup
R.I.P.
42 42
Assigning a new value to "a" without NRVO
Sup
R.I.P.
R.I.P.
43 43
Cleaning up and finishing work
R.I.P.