C++:访问父方法和变量
我应该以什么方式访问这个父方法和父变量?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当存在歧义或其他名称查找问题(例如名称隐藏、模板基类等)时,才需要在代码中使用的两种语法(
this->...
和限定名称) 当没有歧义或其他问题时,您不需要任何这些语法。您所需要的只是一个简单的非限定名称,例如示例中的
Write
。只是Write
,而不是this->Write
,也不是Foundation::Write
。这同样适用于mWords
。即在您的具体示例中,简单的
Write( mWords )
就可以正常工作。为了说明上述情况,如果您的
DoSomething
方法具有mWords
参数,如下所示,则此本地
mWords
参数将隐藏继承的类成员mWords
并且您必须使用或
来正确表达您的意图,即突破隐藏。
如果您的
Child
类也有自己的mWords
成员,如图所示,那么此名称将隐藏继承的
mWords
。在这种情况下,this->mWords
方法不会帮助您取消隐藏正确的名称,并且您必须使用限定名称来解决问题如果您的两个基类都有 < code>mWords 成员,就像在
Child::DoSomething
中那样,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. JustWrite
, notthis->Write
and notFoundation::Write
. The same applies tomWords
.I.e. in your specific example a plain
Write( mWords )
will work just fine.To illustrate the above, if your
DoSomething
method hadmWords
parameter, as inthen this local
mWords
parameter would hide inherited class membermWords
and you'd have to use eitheror
to express your intent properly, i.e. to break through the hiding.
If your
Child
class also had its ownmWords
member, as inthen this name would hide the inherited
mWords
. Thethis->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 problemIf both of your base classes had an
mWords
member, as inthen in
Child::DoSomething
themWords
name would be ambiguous, and you'd have to doto resolve the ambiguity.
But, again, in your specific example, where there's no ambiguity and no name hiding all this is completely unnecessary.
由于不存在命名冲突,因此只需使用
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.我认为这是最常见的方法:
除非您遇到歧义。
如果由于您想要(特定)基类中的某些内容而另一个基类(或此类)中的某些内容隐藏了它而导致存在歧义或隐藏,请使用
Base::name
语法。如果局部变量正在隐藏您的成员之一,请使用
this->
,但一般来说您应该尽量避免这种情况。 (即:尽量避免命名当地人,这样他们就会影子成员)我想查看它的一种方法是使用其中第一个有效并执行您想要的操作:
name
this ->名称
Base::name
I think this is the most common approach:
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:
name
this->name
Base::name