89 lines · plain
1.. title:: clang-tidy - misc-override-with-different-visibility2 3misc-override-with-different-visibility4=======================================5 6Finds virtual function overrides with different visibility than the function7in the base class. This includes for example if a virtual function declared as8``private`` is overridden and declared as ``public`` in a subclass. The9detected change is the modification of visibility resulting from keywords10``public``, ``protected``, ``private`` at overridden virtual functions. The11check applies to any normal virtual function and optionally to destructors or12operators. Use of the ``using`` keyword is not considered as visibility13change by this check.14 15 16.. code-block:: c++17 18 class A {19 public:20 virtual void f_pub();21 private:22 virtual void f_priv();23 };24 25 class B: public A {26 public:27 void f_priv(); // warning: changed visibility from private to public28 private:29 void f_pub(); // warning: changed visibility from public to private30 };31 32 class C: private A {33 // no warning: f_pub becomes private in this case but this is from the34 // private inheritance35 };36 37 class D: private A {38 public:39 void f_pub(); // warning: changed visibility from private to public40 // 'f_pub' would have private access but is forced to be41 // public42 };43 44If the visibility is changed in this way, it can indicate bad design or45programming error.46 47If a virtual function is private in a subclass but public in the base class, it48can still be accessed from a pointer to the subclass if the pointer is converted49to the base type. Probably private inheritance can be used instead.50 51A protected virtual function that is made public in a subclass may have valid52use cases but similar (not exactly same) effect can be achieved with the53``using`` keyword.54 55Options56-------57 58.. option:: DisallowedVisibilityChange59 60 Controls what kind of change to the visibility will be detected by the check.61 Possible values are `any`, `widening`, `narrowing`. For example the62 `widening` option will produce warning only if the visibility is changed63 from more restrictive (``private``) to less restrictive (``public``).64 Default value is `any`.65 66.. option:: CheckDestructors67 68 If `true`, the check does apply to destructors too. Otherwise destructors69 are ignored by the check.70 Default value is `false`.71 72.. option:: CheckOperators73 74 If `true`, the check does apply to overloaded C++ operators (as virtual75 member functions) too. This includes other special member functions (like76 conversions) too. This option is probably useful only in rare cases because77 operators and conversions are not often virtual functions.78 Default value is `false`.79 80.. option:: IgnoredFunctions81 82 This option can be used to ignore the check at specific functions.83 To configure this option, a semicolon-separated list of function names84 should be provided. The list can contain regular expressions, in this way it85 is possible to select all functions of a specific class (like `MyClass::.*`)86 or a specific function of any class (like `my_function` or87 `::.*::my_function`). The function names are matched at the base class.88 Default value is empty string.89