fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h> /* srand, rand */
  4. #include <time.h> /* time */
  5.  
  6. struct foo
  7. {
  8. foo(std::string& str1, std::string& str2) : _str1(str1), _str2(str2) {}
  9.  
  10. ~foo() { std::cout << "Either \"" << _str1 << "\" or \"" << _str2 << "\" was returned." << std::endl; }
  11.  
  12. std::string& _str1;
  13. std::string& _str2;
  14. };
  15.  
  16. std::string foobar()
  17. {
  18. std::string str1("Hello, World!");
  19. std::string str2("Goodbye, cruel World.");
  20. foo f(str1, str2);
  21.  
  22. srand(time(NULL));
  23.  
  24. if (rand() % 2)
  25. {
  26. return str1;
  27. }
  28.  
  29. return str2;
  30. }
  31.  
  32. int main()
  33. {
  34. std::cout << "\"" << foobar() << "\" was actually returned." << std::endl;
  35.  
  36. return EXIT_SUCCESS;
  37. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Either "" or "Goodbye, cruel World." was returned.
"Hello, World!" was actually returned.