vendredi 31 juillet 2015

Do derived classes need to implement move semantics when a base provides it?

I finished reading Thomas Becker's "C++ Rvalue References". I have a couple questions on Rvalues and Rvalue references.

Suppose I have a simple array class:

template <class T>
MyArray
{
    ...
    T* m_ptr;
};

Further suppose it provides:

#if(__cplusplus >= 201103L)
MyArray(MyArray&& t)
{
    std::swap(*this, t);
}
MyArray operator=(MyArray&& t)
{
    std::swap(*this, t);
    return *this;
}
#endif

Now, suppose I have a derived class that does not add new data members:

MyImprovedArray : public MyArray
{
    ...
};

What is required of MyImprovedArray?

Does it need a MyImprovedArray(MyImprovedArray&&) and MyImprovedArray& operator=(MyImprovedArray&&) also? If so, does it only need to perform the base class std::move? Or does it need to perform the std::swap too?

MyImprovedArray(MyImprovedArray&& t)
    : MyArray(std::move(t))
{
}

Aucun commentaire:

Enregistrer un commentaire