Ben Pope wrote:
Metaosp wrote: In the following code:
class Foo {
public:
Foo(string& foo, string& bar) : foo(foo), bar(bar) {}
private:
string foo, bar;
};
Is this argument naming a common practice or considered evil? Or even
an undefined behavior?
It's defined. It could be confusing for anybody reading it.
I, and many others prefer to append an underscore to member variable names:
class Foo {
public:
Foo(const std::string& foo, const std::string& bar) :
foo_(foo),
bar_(bar)
{}
private:
std::string foo_;
std::string bar_;
};
The appended underscore used to be my preferred way of distinguishing
member variables from other variables (I like it better than a prefix
like m_ ). A nice side effect was the disambiguation of the names in
the constructor as compared to the OP's legal but potentially confusing
code.
But after a while, I discovered that this disambiguation in
constructors was almost the _only_ benefit. Inside the bodies of the
class's member functions, the wart distinguishing member variables made
the code slightly harder to read and type for no gain.
So I changed my style to put the wart on the constructor parameter and
leave the member variable name untouched. Hence
class Foo {
public:
Foo(const std::string& foo_, const std::string& bar_) :
foo(foo_),
bar(bar_)
{}
private:
std::string foo;
std::string bar;
};
Just my £0.02. YMMV.
Gavin Deane