#include <iostream>
class X
{
public:
X(const std::string& name) : _name(name) { }
std::string& name() { return _name; }
const std::string& name() const { return _name; }
private:
std::string _name;
};
int main()
{
X *p = new X("Mate");
const X *cp = p;
const std::string &name = cp->name();
std::cout << name << std::endl; // ok
delete p;
std::cout << name << std::endl; // possible segmentation fault: dangling reference
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKY2xhc3MgWAp7CnB1YmxpYzoKICBYKGNvbnN0IHN0ZDo6c3RyaW5nJiBuYW1lKSA6IF9uYW1lKG5hbWUpIHsgfQogICAgICAgIHN0ZDo6c3RyaW5nJiBuYW1lKCkgICAgICAgeyByZXR1cm4gX25hbWU7IH0KICBjb25zdCBzdGQ6OnN0cmluZyYgbmFtZSgpIGNvbnN0IHsgcmV0dXJuIF9uYW1lOyB9CnByaXZhdGU6CiAgc3RkOjpzdHJpbmcgX25hbWU7Cn07CgppbnQgbWFpbigpCnsKICBYICpwID0gbmV3IFgoIk1hdGUiKTsKICBjb25zdCBYICpjcCA9IHA7CiAgY29uc3Qgc3RkOjpzdHJpbmcgJm5hbWUgPSBjcC0+bmFtZSgpOwoKICBzdGQ6OmNvdXQgPDwgbmFtZSA8PCBzdGQ6OmVuZGw7ICAvLyBvawogIGRlbGV0ZSBwOwogIHN0ZDo6Y291dCA8PCBuYW1lIDw8IHN0ZDo6ZW5kbDsgIC8vIHBvc3NpYmxlIHNlZ21lbnRhdGlvbiBmYXVsdDogZGFuZ2xpbmcgcmVmZXJlbmNlCgogIHJldHVybiAwOwp9