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
C++: Accessing parent methods and variables
提问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 Write
in your example. Just Write
, not this->Write
and 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 DoSomething
method had mWords
parameter, as in
为了说明上述情况,如果您的DoSomething
方法有mWords
参数,如
DoSomething(int mWords) {
...
then this local mWords
parameter would hide inherited class member mWords
and 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 Child
class also had its own mWords
member, as in
如果您的Child
班级也有自己的mWords
成员,如
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
那么这个名字会隐藏继承的mWords
. this->mWords
这种情况下的方法不会帮助您取消隐藏专有名称,您必须使用限定名称来解决问题
DoSomething(int mWords) {
Write(Foundation::mWords);
}
If both of your base classes had an mWords
member, as in
如果您的两个基类都有一个mWords
成员,如
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
然后在Child::DoSomething
该mWords
名称将是明确的,并且你必须做
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::name
syntax.
如果存在歧义或阴影,因为您想要(特定)基类中的某些内容,但另一个基类(或此类)中的某些内容隐藏了它,则使用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:
我想一种看待它的方法是使用其中第一个有效并执行您想要的操作:
name
this->name
Base::name
name
this->name
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。