C++ 为什么我应该使用“using”关键字来访问我的基类方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1896830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:31:18  来源:igfitidea点击:

Why should I use the "using" keyword to access my base class method?

c++oopinheritanceusing

提问by Julien Vaslet

I wrote the below code in order to explain my issue. If I comment the line 11 (with the keyword "using"), the compiler does not compile the file and displays this error: invalid conversion from 'char' to 'const char*'. It seems to not see the method void action(char)of the Parentclass in the Sonclass.

我写了下面的代码来解释我的问题。如果我评论的第11行(与“使用”关键字),编译器不编译文件并显示此错误:invalid conversion from 'char' to 'const char*'。它似乎没有看到法void action(char)中的Parent类的Son类。

Why the compiler behave this way? Or have I done something wrong?

为什么编译器会这样?还是我做错了什么?

class Parent
{
    public:
        virtual void action( const char how ){ this->action( &how ); }
        virtual void action( const char * how ) = 0;
};

class Son : public Parent
{
    public:
        using Parent::action; // Why should i write this line?
        void action( const char * how ){ printf( "Action: %c\n", *how ); }
};

int main( int argc, char** argv )
{
    Son s = Son();
    s.action( 'a' );
    return 0;
}

采纳答案by sth

The actiondeclared in the derived class hides the actiondeclared in the base class. If you use actionon a Sonobject the compiler will search in the methods declared in Son, find one called action, and use that. It won't go on to search in the base class's methods, since it already found a matching name.

action派生类的生皮的声明action在基类声明。如果actionSon对象上使用,编译器将在 中声明的方法中搜索Son,找到一个名为 的方法action,然后使用它。它不会继续搜索基类的方法,因为它已经找到了匹配的名称。

Then that method doesn't match the parameters of the call and you get an error.

然后该方法与调用的参数不匹配,您会收到错误消息。

See also the C++ FAQfor more explanations on this topic.

有关此主题的更多解释,另请参阅C++ 常见问题解答

回答by Amnon

Surprisingly this is standard behavior. If a derived class declares a method with the same name as a method defined by the base class, the derived class' method hides the base class' one.

令人惊讶的是,这是标准行为。如果派生类声明的方法与基类定义的方法同名,则派生类的方法会隐藏基类的方法。

See C++ FAQ

请参阅C++ 常见问题解答

回答by Dale Wilson

A note of caution: The need to use a "using" in this situation is a red flag that your code may be confusing for other developers (after all it confused the compiler!). It is likely that you should rename one of the two methods to make the distinction clear to other programmers.

注意事项:在这种情况下需要使用“using”是一个危险信号,您的代码可能会让其他开发人员感到困惑(毕竟它混淆了编译器!)。您可能应该重命名这两种方法之一,以便其他程序员清楚区分。

One possibility:

一种可能:

void action( const char how )
{ 
  takeAction( &how ); 
}
void action( const char * how )
{
  takeAction(how);
}
virtual void takeAction(const char * how) = 0;

回答by Bhupesh Pant

If in a derived class any over loaded function is redefined then all the overloaded function in the base class is hidden. One way to include both the functionality is to avoid function overloading in classes. or You can use usingkeyword, as used.

如果在派生类中重新定义了任何重载函数,则隐藏基类中的所有重载函数。包含这两种功能的一种方法是避免类中的函数重载。或您可以使用using关键字,如使用。