brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 80932c4 Raw
58 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-virtual-class-destructor2 3cppcoreguidelines-virtual-class-destructor4===============================================5 6Finds virtual classes whose destructor is neither public and virtual7nor protected and non-virtual. A virtual class's destructor should be specified8in one of these ways to prevent undefined behavior.9 10This check implements11`C.35 <http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rc-dtor-virtual>`_12from the C++ Core Guidelines.13 14Note that this check will diagnose a class with a virtual method regardless of15whether the class is used as a base class or not.16 17Fixes are available for user-declared and implicit destructors that are either18public and non-virtual or protected and virtual. No fixes are offered for19private destructors. There, the decision whether to make them private and20virtual or protected and non-virtual depends on the use case and is thus left21to the user.22 23Example24-------25 26For example, the following classes/structs get flagged by the check since they27violate guideline **C.35**:28 29.. code-block:: c++30 31  struct Foo {        // NOK, protected destructor should not be virtual32    virtual void f();33  protected:34    virtual ~Foo(){}35  };36 37  class Bar {         // NOK, public destructor should be virtual38    virtual void f();39  public:40    ~Bar(){}41  };42 43This would be rewritten to look like this:44 45.. code-block:: c++46 47  struct Foo {        // OK, destructor is not virtual anymore48    virtual void f();49  protected:50    ~Foo(){}51  };52 53  class Bar {         // OK, destructor is now virtual54    virtual void f();55  public:56    virtual ~Bar(){}57  };58