"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:d19ci5$avi $02$1@news.t-online.com...[color=blue]
> pmatos wrote:
>[color=green]
> > Hi all,
> >
> > What's the difference between:
> > int& i;
> >
> > and
> >
> > int i;
> >
> > ?[/color]
>
> The former is a reference to an integer, the latter is an integer.[/color]
Also former is not legal C++, a reference has to be initialized for sure.
Sharad Kala wrote:[color=blue]
> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
> news:d19ci5$avi $02$1@news.t-online.com...
>[color=green]
>>pmatos wrote:
>>
>>[color=darkred]
>>>Hi all,
>>>
>>>What's the difference between:
>>>int& i;
>>>
>>>and
>>>
>>>int i;
>>>
>>>?[/color]
>>
>>The former is a reference to an integer, the latter is an integer.[/color]
>
>
> Also former is not legal C++, a reference has to be initialized for sure.[/color]
It us legal if it appears inside a class definition.
"Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message[color=blue][color=green]
> > Also former is not legal C++, a reference has to be initialized for[/color][/color]
sure.[color=blue]
>
> It us legal if it appears inside a class definition.[/color]
Yes.
To OP: Even then you need to bind the reference in the initializer list.
Sharad Kala wrote:
[color=blue]
>
> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
> news:d19ci5$avi $02$1@news.t-online.com...[color=green]
>> pmatos wrote:
>>[color=darkred]
>> > Hi all,
>> >
>> > What's the difference between:
>> > int& i;
>> >
>> > and
>> >
>> > int i;
>> >
>> > ?[/color]
>>
>> The former is a reference to an integer, the latter is an integer.[/color]
>
> Also former is not legal C++,[/color]
Wrong. The following should compile:
struct X
{
X(int& i) : i(i) {}
int& i;
};
int main()
{
int i = 3;
X x(i);
}
[color=blue]
> a reference has to be initialized for sure.[/color]
"Rolf Magnus" <ramagnus@t-online.de> .[color=blue][color=green]
> >
> > Also former is not legal C++,[/color]
>
> Wrong.[/color]
Not totally wrong.
int main()
{
int& i; // Not legal
}
Given the context in which OP asked the question, it seemed that it was not
in a class definition. The point I was trying to stress was that you cannot
leave the reference uninitialized.
Sharad Kala wrote:
[color=blue]
>
> "Rolf Magnus" <ramagnus@t-online.de> .[color=green][color=darkred]
>> >
>> > Also former is not legal C++,[/color]
>>
>> Wrong.[/color]
>
> Not totally wrong.
> int main()
> {
> int& i; // Not legal
> }[/color]
Well, can you name one language construct that can be used _everywhere_ in a
program?
[color=blue]
> Given the context in which OP asked the question, it seemed that it was
> not in a class definition.[/color]
Actually, I don't see any context.
[color=blue]
> The point I was trying to stress was that you cannot leave the reference
> uninitialized.[/color]
* Rolf Magnus:[color=blue]
>
> Actually, I don't see any context.[/color]
Note the semicolon at the end in the Original Posting:
int& i;
This is only valid in a class definition.
But since the question was what does it mean, it probably doesn't
imply very much about context...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote:[color=blue]
> * Rolf Magnus:[color=green]
> >
> > Actually, I don't see any context.[/color]
>
> Note the semicolon at the end in the Original Posting:
>
> int& i;
>
> This is only valid in a class definition.
>
> But since the question was what does it mean, it probably doesn't
> imply very much about context...
>[/color]
Indeed, I didn't want to know about context specific questions but I
didn't know it had to be initialized until before reading section 5.5
of C++PL. Anyway, I already understood what it is for. :)
Thanks a lot people and don't get mad about context specific
questions... :)
Cheers,
Paulo Matos
[color=blue]
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?[/color]
Comment