[ create a new paste ] login | about

Link: http://codepad.org/2mnuXVlJ    [ raw code | output | fork ]

johannes - C++, pasted on Sep 15:
class Stuff
{
public:
    Stuff() {}
    ~Stuff() {}

    inline void setSomeStr(const std::string &str) { m_someStr = str; }
    inline const std::string &someStr() const { return m_someStr; }

private:
    std::string m_someStr;
};

int main()
{
    Stuff obj;
    obj.setSomeStr("banan");
    std::cout << obj.someStr() << "\n";

    std::string &evil = const_cast<std::string&>(obj.someStr());
    evil = "hello";
    std::cout << obj.someStr() << "\n";     // obj has now changed!
    return 0;
}


Output:
1
2
banan
hello


Create a new paste based on this one


Comments: