[英]C++ Compiler (cl) does not see parent virtual method with same child method name
I have a C++ code, using inheritance and function overriding at the same time, here is the code: 我有一个C ++代码,同时使用继承和函数重写,这是代码:
#include <iostream>
#include <string>
using namespace std;
class Parent
{
protected:
virtual void G() const = 0;
public:
virtual void G(const string& s) const final { G(); }
};
class Child : public Parent
{
protected:
virtual void G() const override { cout<<"Child G"; }
};
int main()
{
Child *c = new Child();
c->G("test");
return 0;
}
When compile, I got error: Child::G: function does not take 1 arguments
. 编译时出现错误:
Child::G: function does not take 1 arguments
。 But when I use Parent pointer like this: 但是当我像这样使用Parent指针时:
Parent *c = new Child();
It works. 有用。 Alternatively if I change public
G
method's name, it works too. 另外,如果我更改公共
G
方法的名称,它也可以工作。
What is wrong about using same name ( G
) for both methods? 两种方法使用相同的名称(
G
)有什么问题?
You need to introduce the parent member into the child with a using
declaration: 您需要
using
声明将父成员介绍给孩子:
class Child : public Parent
{
protected:
virtual void G() const override { cout<<"Child G"; }
public:
using Parent::G;
};
The fix for this is indeed to introduce the Parent
's method into the scope of the Child
class with a using
declaration, as @Jans kindly points out. 解决此问题的方法的确是
using
声明指出,将Parent
的方法通过using
声明引入Child
类的范围。 As to why this is the case is simply a matter of how the compiler searches through scopes when searching for a method to match your function call. 至于为什么会这样,仅是编译器在搜索与您的函数调用匹配的方法时如何在范围内搜索的问题。 A break down of what's happening is as follows:
发生的情况如下:
Child *c = new Child(); c->G("test");
Child *c = new Child(); c->G("test");
Child *c = new Child(); c->G("test");
, the compiler sees a call to some method G
on an object of type Child
. Child
类型的对象的某些方法G
的调用。 It then searches the scope of Child
to look for a match. Child
的范围以查找匹配项。 Child
, sees only Child::G() const
. Child
的范围时,仅看到Child::G() const
。 It does not see Parent::G(const std::string&) const
, which even though you want it to be included via inheritance, is in a different scope . Parent::G(const std::string&) const
,即使您希望通过继承将其包含在内,也处于不同的范围内 。 In a sense, Child::G
is shadowing Parent::G
. Child::G
遮盖了 Parent::G
Without a candidate match, the compiler would have kept searching into the Parent
scope. Parent
范围。 Child::G
. Child::G
However, this is a function accepting no arguments, and you tried to call it with "test"
. "test"
对其进行调用。 The function call then fails because of a mismatch in the parameters. As stated, you need to bring Parent::G
into the same scope as Child::G
for overloading to happen as intended, with using Parent::G
inside the body of Child
. 如前所述,您需要将
Parent::G
置于与Child::G
相同的作用域内,以便按预期进行重载,并在Child
体内using Parent::G
Source: https://isocpp.org/wiki/faq/strange-inheritance#overload-derived 资料来源: https : //isocpp.org/wiki/faq/strange-inheritance#overload-derived
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.