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

发布于 2024-10-09 00:23:28 字数 460 浏览 5 评论 0原文

我应该以什么方式访问这个父方法和父变量?

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 );
    }
};

谢谢。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-10-16 00:23:28

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

您不需要任何这些语法。您所需要的只是一个简单的非限定名称,例如示例中的 Write 。只是Write,而不是this->Write,也不是Foundation::Write。这同样适用于mWords

即在您的具体示例中,简单的 Write( mWords ) 就可以正常工作。


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

DoSomething(int mWords) {
  ...

,则此本地 mWords 参数将隐藏继承的类成员 mWords 并且您必须使用

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

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

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


如果您的 Child 类也有自己的 mWords 成员,如图所示

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

,那么此名称将隐藏继承的 mWords。在这种情况下,this->mWords 方法不会帮助您取消隐藏正确的名称,并且您必须使用限定名称来解决问题

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

如果您的两个基类都有 < code>mWords 成员,就像在

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

class Foundation {
public:
  int mWords;
  ...

Child::DoSomething 中那样,mWords 名称将是不明确的,您必须采取措施

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

来解决歧义。


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

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.

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 Write in your example. Just Write, not this->Write and not Foundation::Write. The same applies to mWords.

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


To illustrate the above, if your DoSomething method had mWords parameter, as in

DoSomething(int mWords) {
  ...

then this local mWords parameter would hide inherited class member mWords and you'd have to use either

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 Child class also had its own mWords member, as in

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

then this name would hide the inherited mWords. The this->mWords approach 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

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

If both of your base classes had an mWords member, as in

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

class Foundation {
public:
  int mWords;
  ...

then in Child::DoSomething the mWords name would be ambiguous, and you'd have to do

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.

魂牵梦绕锁你心扉 2024-10-16 00:23:28

由于不存在命名冲突,因此只需使用 Write(mWords) 即可。如果局部变量发生冲突或者名称被隐藏,请使用另外两个。

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.

情仇皆在手 2024-10-16 00:23:28

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

Write(mWords);

除非您遇到歧义。

如果由于您想要(特定)基类中的某些内容而另一个基类(或此类)中的某些内容隐藏了它而导致存在歧义或隐藏,请使用 Base::name 语法。

如果局部变量正在隐藏您的成员之一,请使用 this->,但一般来说您应该尽量避免这种情况。 (即:尽量避免命名当地人,这样他们就会影子成员)

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

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

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::name syntax.

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)

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文