fork download
  1. #include <iostream>
  2.  
  3. class X
  4. {
  5. public:
  6. X(const std::string& name) : _name(name) { }
  7. std::string& name() { return _name; }
  8. const std::string& name() const { return _name; }
  9. private:
  10. std::string _name;
  11. };
  12.  
  13. int main()
  14. {
  15. X *p = new X("Mate");
  16. const X *cp = p;
  17. const std::string &name = cp->name();
  18.  
  19. std::cout << name << std::endl; // ok
  20. delete p;
  21. std::cout << name << std::endl; // possible segmentation fault: dangling reference
  22.  
  23. return 0;
  24. }
Runtime error #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
Mate