C & C++

C & C++

Made by DeepSource

Missing or incorrect invocation of base class copy constructor CXX-W2003

Bug risk
Major
Autofix

An inheriting class with a user-defined copy constructor must call its base class's copy constructor.

Bad practice

class BaseCopy {
public:
  BaseCopy() = default;
  BaseCopy(const BaseCopy &) = default;
};
class TheDerivedOne : public BaseCopy {
  // BaseCopy(D) is missing
  TheDerivedOne(const TheDerivedOne &D) {}
};

Or

class IDeriveToo : public BaseCopy {
  // D is missing
  IDeriveToo(const IDeriveToo &D) : BaseCopy() {}
};

Recommended

class BaseCopy {
public:
  BaseCopy() = default;
  BaseCopy(const BaseCopy &) = default;
};
class TheDerivedOne : public BaseCopy {
  // BaseCopy(other) is missing
  TheDerivedOne(const TheDerivedOne &D) : BaseCopy(D) {}
};