TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  INT#026Checked automatically with code checker

Synopsis:In a derived class, if you need to override one of a set of the base class's overloaded virtual member functions, then you must override the whole set, or use using-declarations to bring all of the functions in the base class into the scope of the derived class
Language:C++
Severity Level:2
Category:Class Interface


Description:

Overload resolution is not applied across different class scopes. As a result, ambiguities between functions from base class and derived class are not resolved based on argument types. In other words, if a function is defined in a base class and overloaded in the derived, then the function in the derived class will be called if the argument can be converted, even if the base class defines a function with the correct type of argument.

// Incorrect!!
class Base
{
   public:
      DoSomething(char c);
};

class Derived: public Base
{
   public:
      DoSomething(int i);
}

void
MyClass::f()
{
   char c = 'a';
   int i = 100;
   Derived d;

   d.DoSomething(i);    // will call Derived::DoSomething(int)
   d.DoSomething(c);    // will unexpectedly call Derived::DoSomething(int)
}

Probably the next behaviour was meant

// Correct
class Base
{
   public:
      DoSomething(char c);
};

class Derived: public Base
{
   public:
      using Base::DoSomething;
      DoSomething(int i);
};

void
MyClass::f()
{
   char c = 'a';
   int i = 100;
   Derived d;

   d.DoSomething(i);    // will call Derived::DoSomething(int)
   d.DoSomething(c);    // will call Base::DoSomething(char)
}


Literature References:
ISC++ Rule 7.16
Stroustrup 15.2.2