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
1 class Base 2 { 3 public: 4 mf (void); 5 }; 6 7 class Derived : public Base 8 { 9 public: 10 mf (void); 11 }; 12 13 example_function(void) 14 { 15 Derived derived; 16 Base* base_ptr = &derived; // Points to derived 17 Derived* derived_ptr = &derived; // Points to derived 18 base_ptr->mf(); // Calls Base::mf() *** Different behavior for same object!! 19 derived_ptr->mf(); // Calls Derived::mf() 20 }