class Foo {
private:
int num;
std::string str;
public:
Foo& operator=(const Foo& rhs) {
this->num = rhs.num;
this->str = rhs.str;
return *this;
}
Foo& operator+(const Foo& rhs) {
Foo tmp(this->num + rhs.num, this->str + rhs.str);
return tmp;
}
};
If I write a code like Foo f3; f3 = f1 + f2;
, runtime error occurs when this->str = rhs.str;
in operator=() is executed.
I think temporary std::string object(this->str + rhs.str
) made in operator+() is regional so it is removed when the function call is finished. But int value(this->num + rhs.num
) is passed well without problems.
Why is this difference happening?