Re: reference type methods
On Jul 15, 5:12 am, Ruben <ru...@www2.mrbrklyn.com> wrote:
On Mon, 14 Jul 2008 23:36:45 +1200, Ian Collins wrote:
Ruben wrote:
On Mon, 14 Jul 2008 23:26:07 +1200, Ian Collins wrote:
But I still am not sure why it doesn't cause a type
mismatch error.
Because there isn't one.
The type of _array[index] is int. The function returns a
reference to an int, so there isn't any mismatch.
That is the part I don't satisfactorly understand. Why is
it not returning the rvalue of the array element.
That would be const reference.
That's not really true either. A const reference is also an
lvalue.
Why is it different than this
int x, y[10] = {1,2,3,4,5,6,7,8,9,10};
x = y[3];
How is it different? It is more like
int& x = y[3];
I would think the requested return value doen't match the method
definition and I'd have to return something like this
return &i[index];
No, you are out by one level of indirection. &i[index]
takes the address of i[index] and yields a pointer to int.
Yes but i[index] returns a literal integer.
No. By definition, i[index] is *(i + index), and the results of
a dereference operator is always an lvalue. There's no literal
involve here at all.
In fact, I don't know of any way of returning a varriable,
only the data that a symbolic variable refers to.
You can't return a variable, however... A certain number of
expressions result in lvalues, both the name of a variable and
dereferencing, for example. An lvalue refers to an object; if
you need the value, the compiler automatically applies an lvalue
to rvalue conversion, but only if you need the value. You can
take the address of an lvalue, and you can use it to initialize
a reference. If you use it to initialize a reference, the
reference refers to the object the lvalue expression referred
to.
(You can also use a rvalue to initialize a reference to const.
In that case, the compiler automatically generates a temporary
object of the required type, and initializes the reference to
refer to it.)
I do know how to return an address of a variables assigned
space, though a pointer, like in scanf for example.
return i[index] isn't an lvalue.
i[index] is an lvalue.
Yet when the fuction is defined as returning a reference, it
seems to have a seperate syntax unrelated to the rest of a
references implimentation. The only way I know of returning
an actually reference...actually I don't know a way because
even under this circumstance
int =y =3, z;
int & x = y;
z = x; //STILL an RVALUE assigned and copied to z
x is an lvalue. There's an implicit lvalue to rvalue conversion
which occurs here.
so
return x;//should still be an rvalue returned (unless you return a point)
This seems to be a seperate property of references.
It has nothing to do with references per se. You seem to be
having problems with which expressions are lvalues, and which
aren't. From memory: id expressions (the name of an object),
subscripting, dereferencing, prefix increment and decrement, and
the assignment expression, are lvalues. Other expressions may
be lvalues in certain cases: a function call or a type
conversion (cast) is an lvalue if and only if the return
type/converted to type is a reference, and there are likely a
few special cases I've forgotten.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34