C ++:访问父方法和变量 [英] C++: Accessing parent methods and variables

查看:129
本文介绍了C ++:访问父方法和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该以何种方式访问​​此父方法和父变量?

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

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

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

感谢。



如果有歧义?

解决方案

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



当没有歧义或其他问题,你不需要任何这些语法。所有你需要的是一个简单的非限定名称,例如 Write 。只需写,而不是 this->写,而不是 Foundation :: Write 。这同样适用于 mWords



在你的具体示例中,一个简单的 Write(mWords)将正常工作。





$ b b

为了说明上述情况,如果 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);
}






类有 mWords 成员,如

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

class Foundation {
public:
int mWords;
...

然后在 Child :: DoSomething mWords 名称将不明确,您必须执行

  DoSomething(int mWords){
Write(Foundation :: 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?

解决方案

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.

这篇关于C ++:访问父方法和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆