名称隐藏在构造函数初始化列表中 [英] Name hiding in constructor initialization list
本文介绍了名称隐藏在构造函数初始化列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想修改构造函数以使用初始化列表,如以下示例所示:
I want to modify a constructor to use an initialization list as in the following example:
class Foo
{
public:
Foo(std::wstring bar);
private:
std::wstring bar;
};
// VERSION 1:
Foo::Foo(std::wstring bar) {this->bar = bar}
// VERSION 2:
Foo::Foo(std::wstring bar) : this->bar(bar) {} // ERROR!
<这个数据成员的指针,因为(我猜),他们在那一点还不存在。
Unfortunately I can't do version 2 because you can't use the this
pointer for data members since (I'm guessing) they don't exist yet at that point. How then, do I deal with the name hiding issue (i.e. my parameter and my data member have the same name)?
推荐答案
如何处理名称隐藏问题(即我的参数和我的数据成员具有相同的名称)你不需要。第一个 bar
将引用成员,第二个 bar
将引用参数:
You don't need to. The first bar
will refer to the member and the second bar
will refer to the argument:
Foo::Foo(std::wstring bar) : bar(bar) {}
这篇关于名称隐藏在构造函数初始化列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文