Woverloaded-virtual warning with the usual method

I am confused why the following code creates a Woverloaded-virtual warning.

class TestVirtual
{
public:
    TestVirtual();
    virtual void TestMethod(int i);
};

class DerivedTestVirtual : public TestVirtual
{
public:
    void TestMethod();

};

The derived class has the usual TestMethod method without parameters - the signature is different from the same virtual method of the base class. Then why the compiler cannot solve this situation?

+4
source share
1 answer

The reason for the warning is that the version without parameters hides the version intfrom the base class.

DerivedTestVirtual tdv;
tdv.TestMethod(0); // This line will cause an error.

You can get around this by declaring that you are using all the source overloads from the database, for example:

class DerivedTestVirtual : public TestVirtual
{
public:
    using TestVirtual::TestMethod;
    void TestMethod();
};

. , , , . override, .

+5

Source: https://habr.com/ru/post/1685107/


All Articles