calling a base constructor other than the copy constructor
20
21class Derived : public Base {
22 // Consider calling base copy ctor: Derived(const Derived &other) : Base(other){}
23 Derived(const Derived &other) {}24};
25
26float foo(float a[100]) {
Description
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) {}
};