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)?
You don’t need to. The first
barwill refer to the member and the secondbarwill refer to the argument: