On Thu, 2010-12-30, Ian Collins wrote:
> On 12/30/10 08:42 PM, Nick Keighley wrote:
....
>> class Thing
>> {
>> private:
>> int i_;
>>
>> public:
>> Thing(int i): i_(i)
>> {}
>> };
>>
>> I need different names for the parameter and the member variable.
>
> That example is the classic case where you don't!
>
> The ambiguity arises when you use the member variable in the constructor
> body.
That surprises me -- are you really sure, and do you have a reference
handy? I'm not sure you can find it out TC++PL (which is all the
literature I own).
No, don't bother -- a test with gcc makes it clear what's going on.
foo.cc: In constructor 'Foo::Foo(int, int)':
foo.cc:4: error: class 'Foo' does not have any field named 'b'
The i_ in 'Thing(int i): i_(i)' really *does* have to refer to a
member of Thing. It's clearly not just any lvalue, as I guess I
believed.
This is really good news -- I'm frequently annoyed when I have to come
up with unnatural names. Normally I use the foo_ convention for
members so it's not a problem. I don't for struct-like types where all
members are public however, so either I come up with alternate
spellings or abbreviations, or I invert my own rule and use the _
suffix for the parameter name instead.
I will think of this tip as a slightly delayed Christmas gift
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .