JSF.DERIVED.NON_VIRT.REDEFINED

An inherited nonvirtual function shall not be redefined in a derived class.

Rationale

Prevent an object from exhibiting “two-faced” behavior.

Vulnerability and risk

Nonvirtual functions are statically bound. In essence, a nonvirtual function will hide its corresponding base class version. Hence a single derived class object may behave either as a base class object or as a derived class object depending on the way in which it was accessed—either through a base class pointer/reference or a derived class pointer/reference.

Mitigation and prevention

To avoid this duality in behavior, nonvirtual functions should never be redefined.

Example 1

Copy
  class Base
  {
  public:
      mf (void);
  };
  
  class Derived : public Base
  {
  public:
     mf (void);
 };
 
 example_function(void)
 {
     Derived derived;
     Base* base_ptr = &derived; // Points to derived
     Derived* derived_ptr = &derived; // Points to derived
     base_ptr->mf(); // Calls Base::mf() *** Different behavior for same object!!
     derived_ptr->mf(); // Calls Derived::mf()
 }