#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(void) const { cout << "A::foo(void)" << endl; }
virtual void foo(int i) const { cout << i << endl; }
virtual ~A() {}
};
class B : public A
{
public:
void foo(int i) const { this->foo(); cout << i << endl; }
};
class C : public B
{
public:
void foo(void) const { cout << "C::foo(void)" << endl; }
};
int main(int argc, char ** argv)
{
C test;
test.foo(45);
return 0;
}
上面的代码不能编译:
$>g++ test.cpp -o test.exe
test.cpp: In member function 'virtual void B::foo(int) const':
test.cpp:17: error: no matching function for call to 'B::foo() const'
test.cpp:17: note: candidates are: virtual void B::foo(int) const
test.cpp: In function 'int main(int, char**)':
test.cpp:31: error: no matching function for call to 'C::foo(int)'
test.cpp:23: note: candidates are: virtual void C::foo() const
如果方法“foo(void)”更改为“goo(void)”,它将编译。为什么会这样?是否可以在不更改“foo(void)”的方法名称的情况下编译代码?
谢谢。