2

Suppose I have the struct A and I want to overload virtual int f() in the derived class B. Here's the full code:

#include <iostream>

struct A
{
    virtual int f() = 0;
    int f(int i) { return i + f(); }
};

struct B : A
{
    int f() override { return 10; }
};


int main()
{
    std::cout << B().f(3) << std::endl;
    return 0;
}

But when I try to compile GCC says:

derived.cpp: In function ‘int main()’:
derived.cpp:17:25: error: no matching function for call to ‘B::f(int)’
derived.cpp:17:25: note: candidate is:
derived.cpp:11:9: note: virtual int B::f()
derived.cpp:11:9: note:   candidate expects 0 arguments, 1 provided

Clang cannot process the code, too.

But if I try to rename all the int f() functions to int g() everything works fine. What's the reason of such behaviour?

0 Answers0