Main Content

Partial override of overloaded virtual functions

Class overrides fraction of inherited virtual functions with a given name

Description

This defect occurs when:

  • A base class has multiple virtual methods with the same name but different signatures (overloading).

  • A class derived from the base class overrides at least one of those virtual methods, but not all of them.

Risk

The virtual methods that the derived class does not override are hidden. You cannot call those methods using an object of the derived class.

Fix

See if the overloads in the base class are required. If they are needed, possible solutions include:

  • In your derived class, if you override one virtual method, override all virtual methods from the base class with the same name as that method.

  • Otherwise, add the line using Base_class_name::method_name to the derived class declaration. In this way, you can call the base class methods using an object of the derived class.

Examples

expand all

class Base {
public:
    explicit Base(int b);
    virtual ~Base() {};
    virtual void set()           {
        _b = (int)0;
    };
    virtual void set(short i)    {
        _b = (int)i;
    };
    virtual void set(int i)      {
        _b = (int)i;
    };
    virtual void set(long i)     {
        _b = (int)i;
    };
    virtual void set(float i)    {
        _b = (int)i;
    };
    virtual void set(double i)   {
        _b = (int)i;
    };
private:
    int _b;
};

class Derived: public Base {             
        public:
                Derived(int b, int d): Base(b), _d(d) {};
                void set(int i)    { Base::set(i); _d = (int)i; };
        private:
                int _d;
};

In this example, the class Derived overrides the function set that takes an int argument. It does not override other functions that have the same name set but take arguments of other types.

The defect appears on the derived class name in the derived class definition. To find which base class method is overridden:

  1. Navigate to the base class definition. On the Source pane, right-click the base class name and select Go To Definition.

  2. In the base class definition, identify the method that has the same name and signature as a derived class method name.

Correction — Unhide Base Class Method

One possible correction is add the line using Base::set to the Derived class declaration.

class Base {
public:
    explicit Base(int b);
    virtual ~Base() {};
    virtual void set()           {
        _b = (int)0;
    };
    virtual void set(short i)    {
        _b = (int)i;
    };
    virtual void set(int i)      {
        _b = (int)i;
    };
    virtual void set(long i)     {
        _b = (int)i;
    };
    virtual void set(float i)    {
        _b = (int)i;
    };
    virtual void set(double i)   {
        _b = (int)i;
    };
private:
    int _b;
};

class Derived: public Base {             
        public:
                Derived(int b, int d): Base(b), _d(d) {};
                using Base::set;
                void set(int i)    { Base::set(i); _d = (int)i; };
        private:
                int _d;
};

Result Information

Group: Object oriented
Language: C++
Default: On for handwritten code, off for generated code
Command-Line Syntax: PARTIAL_OVERRIDE
Impact: Medium

Version History

Introduced in R2015b