fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4.  
  5. struct Bar
  6. {
  7. int m_bar;
  8. int &ref_bar;
  9.  
  10. Bar(int bar_, int g) : m_bar(bar_),ref_bar(m_bar)
  11. {;}
  12.  
  13. void print()
  14. {
  15. std::cout<< "m_bar = "<<m_bar<<std::endl;
  16. }
  17. };
  18.  
  19.  
  20. struct Foo
  21. {
  22. std::shared_ptr<Bar> m_foo;
  23.  
  24. Foo() : m_foo( std::make_shared<Bar>(Bar(23,10)) ) // prints 23 23, Wrong!
  25. //Foo() : m_foo( new Bar(23,10) ) // prints 23 12, Correct!
  26. //Foo() : m_foo( std::make_shared<Bar>(23,10) ) // prints 23 12, FIXED!
  27. {
  28. m_foo->print();
  29. m_foo->ref_bar = 12;
  30. m_foo->print();
  31. }
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. Foo f;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
m_bar = 23
m_bar = 23