GeneralPurpose-PureVirtualCallΒΆ

No pure virtual member calls from within its constructor or destructor

Required inputs: IR

Calling pure virtual functions directly or indirectly from a constructor or destructor results in undefined behavior. During construction and destruction, the object's virtual function table points to the current class, not derived classes. Calling pure virtual functions at these points will either fail to link or invoke the base class pure-virtual stub, which typically aborts the program.
Bad code (pure virtual call in constructor):
class Base {
public:
    Base() {
        Setup();  // ERROR: indirect pure virtual call
    }
    virtual void Setup() = 0;
};

class Derived : public Base {
public:
    void Setup() override { }
};
Bad code (pure virtual call in destructor):
class Base {
public:
    virtual ~Base() {
        Cleanup();  // ERROR: pure virtual call in destructor
    }
    virtual void Cleanup() = 0;
};
Good code (using non-virtual initialization):
class Base {
protected:
    void CommonSetup() {
        // Non-virtual initialization in base
    }
public:
    Base() {
        CommonSetup();  // OK: non-virtual
    }
    virtual void Setup() = 0;
};
Good code (move initialization to derived):
class Base {
public:
    virtual ~Base() { }
};

class Derived : public Base {
public:
    Derived() {
        Setup();  // OK: Setup is virtual and overridden
    }
    void Setup() { }
};

Possible Messages

Key

Text

Severity

Disabled

indirect_pure_virtual_call

Pure virtual call (indirectly) called by constructor/destructor.

None

False

Options