Edit

Share via


Warning C26434

Function 'derived::function' hides a non-virtual function 'base::function' (c.128).

C++ Core Guidelines

C.128: Virtual functions should specify exactly one of virtual, override, or final

Remarks

When you introduce a function that has the same name as a non-virtual function in a base class, you may get unexpected behavior. It's like introducing a variable name that conflicts with a name from an outer scope. For example, you may have intended to override a base class function. If the signatures of the functions don't match, the override you intended may turn into an overload instead. In general, name hiding is dangerous and error-prone.

In the Core Guidelines checks:

  • Only non-overriding functions in the current class are checked.
  • Only non-virtual functions of base classes are considered.
  • No signature matching is performed. Warnings are emitted if unqualified names match.

Example

This example demonstrates how a derived class can hide non-virtual functions, and how virtual functions allow both overloads and overrides:

// C26434.cpp
struct Base
{
    virtual ~Base() = default;
    virtual void is_virtual() noexcept {}
    void not_virtual() noexcept {}
};

struct Derived : Base
{
    void is_virtual() noexcept override {}      // Okay, override existing function
    virtual void is_virtual(int i) noexcept {}  // Add a virtual overload for function
    void not_virtual() noexcept {}              // C26434, hides a non-virtual function
    virtual void not_virtual(int i) noexcept {} // C26434, and parameters ignored
};