short question about reference

hy,
for example:
1
2
3
4
float& function (float x) {
    float y = x;
    return y;
}


function return's reference.
reference to what???

how function can have as return type& , I mean when is it used ref as return?
or if u use sobjects or any example how to use this kind of function properly.

WHAT'S THIS:
1
2
3
4
5
6
7
template <class elem>
elem& manjiUNizu (elem* niz, int br) {
	elem* ptrNajmanji = niz;
	for (short i = 1; i < br; i++)
		if (niz [i] < *ptrNajmanji) ptrNajmanji = &niz [i];
	return *ptrNajmanji;
}

what will this function return exactly please explain.
thanks!
Last edited on
closed account (3hM2Nwbp)
Try cranking up your warnings setting. The compiler should give you a warning about returning a temporary.
yeah that's wright for first example..

but second one is ok.
can someone explain me second example please.
what it does return:
string "niz"
or
local string??
closed account (3hM2Nwbp)
The second example is returning a reference to the value at ptrNajmanji - which is a valid memory location on the heap. That reference will be valid until delete is called on that pointer.
Topic archived. No new replies allowed.