Sunday, March 28, 2010

Safer String Implementation

I came across this 'safer' string implementation and had to ask, "How is this 'safer'?"

struct safe_string
{
    safe_string(const std::string & str = std::string())
        : str_ (str.c_str())
    {
    }

    safe_string(const safe_string & copy)
        : str_(copy.str_.c_str())
    {
    }

    safe_string & operator = (const safe_string & copy)
    {
        str_ = copy.str_.c_str();
        return *this;
    }

    std::string           str_;

};

No comments: