Re: Some syntax question
"Jacky" <jl@knight.com> schrieb im Newsbeitrag
news:Oj0Cu$0UHHA.1000@TK2MSFTNGP05.phx.gbl...
I quoted
"Initialization of a reference is trivial when the initializer is an
lvalue (an object whose
address you can take. The initializer for a "plain" T& must be an lvalue
of type T"
What exactly is the lvalue?
Basically, an l-value is something that can be used at the left side of an
assignment statement. For an exact definition, you have to consult the C++
standard. Also, it is not enough that a reference must be initialized with
an l-value. It must also be an l-value with a matching type.
int i;
int& ri = i; // fine, i is an l-value of type int, ri is a reference
to an int
double& rd = i; // error, i is not a double
Some ASCIIart may help :)
double &dr = 1; // error
You cannot assign a value to the number 1, so you cannot initialize a plain
reference with it.
const double& cdr = 1; //ok
This one is fine. Not because 1 suddenly is an l-value, but because cdr is
not a plain reference. It is a const reference (a reference to a constant
object). The compiler is supposed to create a temporary object, initialize
it with 1 and bind the reference to that temporary. However, only const
references can be bound to temporary objects.
HTH
Heinz