| re: Initialisation of reference vs. initialisation of reference member
John Carson wrote:[color=blue]
> "shailesh" <shaileshk@gmail.com> wrote in message
> news:1148984147.251246.260650@j33g2000cwa.googlegr oups.com[color=green]
>>
>> 2:
>>
>> About the error you saw, MSDN says this:
>> 'reference' : initialization of reference member requires a temporary
>> variable
>>
>> A constructor initializes a reference to a member instead of
>> initializing the member.
>>
>> It is invalid to initialize a reference member of a class in the
>> class's constructor with a temporary variable. An attempt to do so
>> generates the C2354 error, as illustrated by this sample code:
>>
>> // C2354.cpp
>> int temp() { return 1; }
>> class Test
>> {
>> public:
>> int member;
>> int& ref_member;
>> Test();
>> };
>>
>> Test::Test() : ref_member( temp() )
>> { // C2354
>> }
>> When this error is encountered, the solution is to change the code so
>> that the reference member is not initialized to a temporary variable.
>> The reference must be initialized to an object that will exist for
>> the lifetime of the reference member.
>> ------------------------------------------------------------------------------------
>>
>> So it seems that the 2 in the constructor call is being treated as a
>> temporary variable. and not as something in the global memory.[/color]
>
> On the other hand, Comeau online compiles the code without complaint.[/color]
Hi John, so does GCC 3.3.4 for ARM and it behaves as expected at run-time
too. |