Calling str
with no argument returns a copy of the internal string object. That's why it's a temporary object. Calling str
with a string argument sets the value of the internal string object. It doesn't work on a temporary object.
It seems like you're thinking of it like this:
oss.str() = "";
But that's not what it is. You're passing ""
to the str
function so that it can assign it to the internal string.
It's no different to any other getter/setter combination. If you have getX()
, it typically gets you a copy of some member of the class you're calling it on. If you have setX(x)
, it typically sets the value of that member. In this case, they're just both called str
, but one takes an argument and the other doesn't.