fork(2) download
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct cache {
  8. bool valid;
  9. string rep;
  10. };
  11.  
  12. class Date {
  13. public:
  14. Date(int dd, int mm, int yy) : d{dd}, m{mm}, y{yy}, c{new cache{false, ""}} {};
  15. ~Date() { delete c; }
  16. string string_rep() const;
  17. private:
  18. int d, m, y;
  19. cache * c;
  20. void compute_cache_value() const;
  21. };
  22.  
  23. string Date::string_rep() const {
  24. if (!c->valid) {
  25. compute_cache_value();
  26. c->valid = true;
  27. }
  28. return c->rep;
  29. }
  30.  
  31. void Date::compute_cache_value() const {
  32. c->rep = "Hello";
  33. }
  34.  
  35. int main() {
  36. Date const d(1, 1, 1970);
  37. cout << d.string_rep();
  38. return 0;
  39. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Hello