jeudi 30 avril 2015

Rule of 5 for base/derived class

Let's say you have:

class Base 
{
public:
    virtual ~Base();
private:
    int someTrivialValue;
}
class Derived : public Base
{
}

  1. I believe creating the virtual destructor in Base is required.
  2. Since a destructor is explicity declared, a default ctor and copy/move should also be explicity defined as per the Rule of 5, even if they only contain primitive data types or even user-defined objects allocated on the stack.
  3. The default ctor, copy and move operations, they have to be explicitly defined in both the base and derived classes.
  4. It is ok to use = default for the default ctor and copy
  5. I am on VS 2013 and it does not allow using =default for move operations. So this means I have to manually write field = std::move(rhs.field) for every single variable in each class.

Please correct any of my above statements if they are incorrect.

Aucun commentaire:

Enregistrer un commentaire