I was wondering if the following code was safe, considering the child object is implicitly converted to type Parent
and then moved from memory. In other words, when passing other
to Parent::operator=(Parent&&)
from Child::operator(Child&&)
, is the entire object "moved" with the parent call, or just the underlying Parent
object?
class Parent
{
public:
// Constructors
Parent& operator=(Parent&& other) noexcept
{
if (this == &other)
return *this;
str1_ = std::move(other.str1_);
str2_ = std::move(other.str2_);
return *this;
}
protected:
std::string str1_, str2_;
};
class Child : public Parent
{
public:
// Constructors
Child& operator=(Child&& other) noexcept
{
if (this == &other)
return *this;
// Are the following 2 lines memory safe?
Parent::operator=(std::move(other));
str3_ = std::move(other.str3_);
return *this;
}
private:
std::string str3_;
};