C++:访问父方法和变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4523545/
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-28 15:38:53  来源:igfitidea点击:

C++: Accessing parent methods and variables

c++inheritanceparent-childbase-class

提问by

In which way should I access this parent method and parent variable?

我应该以哪种方式访问​​这个父方法和父变量?

class Base
{
public:
    std::string mWords;
    Base() { mWords = "blahblahblah" }
};

class Foundation
{
public:
    Write( std::string text )
    {
        std::cout << text;
    }
};

class Child : public Base, public Foundation
{
    DoSomething()
    {
        this->Write( this->mWords );
        // or
        Foundation::Write( Base::mWords );
    }
};

Thanks.

谢谢。

Edit: And what if there is ambiguity?

编辑:如果有歧义怎么办?

采纳答案by AnT

The two syntaxes you use in your code (this->...and qualified names) are only necessary specifically when there is ambiguity or some other name lookup issue, like name hiding, template base class etc.

您在代码中使用的两种语法(this->...和限定名称)仅在存在歧义或其他名称查找问题(如名称隐藏、模板基类等)时才需要。

When there's no ambiguity or other problems you don't need any of these syntaxes. All you need is a simple unqualified name, like Writein your example. Just Write, not this->Writeand not Foundation::Write. The same applies to mWords.

当没有歧义或其他问题时,您不需要任何这些语法。您只需要一个简单的非限定名称,就像Write在您的示例中一样。只是Write,不是this->Write和不是Foundation::Write。这同样适用于mWords.

I.e. in your specific example a plain Write( mWords )will work just fine.

即在你的具体例子中,一个普通的Write( mWords )就可以了。



To illustrate the above, if your DoSomethingmethod had mWordsparameter, as in

为了说明上述情况,如果您的DoSomething方法有mWords参数,如

DoSomething(int mWords) {
  ...

then this local mWordsparameter would hide inherited class member mWordsand you'd have to use either

那么这个本地mWords参数将隐藏继承的类成员mWords,你必须使用

DoSomething(int mWords) {
  Write(this->mWords);
}

or

或者

DoSomething(int mWords) {
  Write(Foundation::mWords);
}

to express your intent properly, i.e. to break through the hiding.

正确表达你的意图,即突破隐藏。



If your Childclass also had its own mWordsmember, as in

如果您的Child班级也有自己的mWords成员,如

class Child : public Base, public Foundation
{
  int mWords
  ...

then this name would hide the inherited mWords. The this->mWordsapproach in this case would not help you to unhide the proper name, and you'd have to use the qualified name to solve the problem

那么这个名字会隐藏继承的mWords. this->mWords这种情况下的方法不会帮助您取消隐藏专有名称,您必须使用限定名称来解决问题

DoSomething(int mWords) {
  Write(Foundation::mWords);
}


If both of your base classes had an mWordsmember, as in

如果您的两个基类都有一个mWords成员,如

class Base {
public:
  std::string mWords;
  ...
};

class Foundation {
public:
  int mWords;
  ...

then in Child::DoSomethingthe mWordsname would be ambiguous, and you'd have to do

然后在Child::DoSomethingmWords名称将是明确的,并且你必须做

DoSomething(int mWords) {
  Write(Foundation::mWords);
}

to resolve the ambiguity.

来解决歧义。



But, again, in your specific example, where there's no ambiguity and no name hiding all this is completely unnecessary.

但是,同样,在您的特定示例中,没有歧义且没有名称隐藏所有这些是完全没有必要的。

回答by Laurence Gonsalves

I think this is the most common approach:

我认为这是最常见的方法:

Write(mWords);

unless you run into ambiguity.

除非你遇到歧义。

If there's ambiguity or shadowing because you want something in a (particular) base class but something in another base class (or this class) hides it, then use the Base::namesyntax.

如果存在歧义或阴影,因为您想要(特定)基类中的某些内容,但另一个基类(或此类)中的某些内容隐藏了它,则使用Base::name语法。

If a local variable is shadowing one of your members, then use this->, though in general you should try to avoid this situation. (ie: try to avoid naming locals such that they shadow members)

如果局部变量隐藏了您的成员之一,则使用this->,但通常您应该尽量避免这种情况。(即:尽量避免命名当地人,使他们影子成员)

I suppose one way to look at it would be to use the first of these that works and does what you want:

我想一种看待它的方法是使用其中第一个有效并执行您想要的操作:

  1. name
  2. this->name
  3. Base::name
  1. name
  2. this->name
  3. Base::name

回答by lijie

Since there is no naming conflict, simply use Write(mWords). Use the other 2 if you have local variables that conflict, or when the names are hidden.

由于没有命名冲突,只需使用Write(mWords). 如果您有冲突的局部变量,或者名称被隐藏,请使用其他 2。