I have a member function like
setResult(const std::string &s)
{
this->m_result = s;
}
After checking, I found this->m_result‘s address is the same as s. Will m_result disappear (or become garbage) when s goes out of scope if s is a stack object?
Probably not; if
m_resultis a std::string, it will deep-copy the data contents.Anyway, in places like assignment operators, you’d normally check against this kind of self-assignment. The idiom is:
The same goes for your case, where you could potentially check it
&m_resultis different from the passed in string. But std::string are usually implemented so that self-copying is OK.