GeneralPurpose-MissingBaseCopyΒΆ

Do call the base class copy constructor or assignment operator, if you implement a copy constructor or assignment operator in a derived class

Required inputs: IR

Copy constructor or assignment operators should explicitly call the base class copy constructor or assignment operator.
Bad code:
class Base {
    int i;
};
class Derived : public Base {
public:
    Derived(const Derived& other) {} // ERROR: Copy constructor should explicitly call copy constructor of its base classes.
    Derived& operator=(const Derived& other) {} // ERROR: Assignment operator should explicitly call assignment operator of its base classes.
};
Good code (explicit calls to base class copy constructor and assignment operator):
class Base {
    int i;
};
class Derived : public Base {
public:
    Derived(const Derived& other) : Base(other) {} // OK: Copy constructor explicitly calls copy constructor of its base class.
    Derived& operator=(const Derived& other) {
        if (this != &other) {
            Base::operator=(other); // OK: Assignment operator explicitly calls assignment operator of its base class.
            // ...
        }
        return *this;
    }
};
Good code (empty base class has trivial copy constructor):
class Base {
public:
    virtual void foo() = 0;
};
class Derived : public Base {
public:
    Derived(const Derived& other) {} // OK: Base class is empty and copy constructor is trivial, so no explicit call is needed.
    void foo() override {}
};

Possible Messages

Key

Text

Severity

Disabled

missing_assignment_call

Assignment operator should explicitly call assignment operator of its base classes.

None

False

missing_constructor_call

Copy constructor should explicitly call copy constructor of its base classes.

None

False

Options