Hi, I'm very confused about how function returns refenrence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours; //return reference to local variable ????
}
int main() {
double a = GetWeeklyHours();
cout << a << endl; //ref to local variable did not get out of scope lol??
return 0;
}
it realy return 46,5 ?!
can someone explain me that behvior please.
Your code is bad. 'h' does in fact go out of scope and therefore GetWeeklyHours returns garbage.
Whether or not the garbage you're getting back is 46.5 is unreliable and unpredictable. You might get 46.5 or you might get -2394293. There's no way to know.
Really, there is no reason to return a reference here. Just return a normal double.
tnx for your reply,
howerver I dont understand what does returning by reference mean then?
how returning by reference work and when sould I return by reference?
obviously the code bellow has no sence:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
double & GetWeeklyHours(double& x) {
return x;
}
int main() {
double s = 2;
double a = GetWeeklyHours(s);
cout << a << endl;
system("pause");
return 0;
}
No, his first example is wrong. The reference is dereferenced in main, at which point the variable it refers to is no longer in scope, therefore it's a bad dereference and is undefined behavior.
howerver I dont understand what does returning by reference mean then?
It's typically only done for container classes to return an element within the container. For example, vector's [] operator returns a reference.
Returning a reference allows the returned value to be "assigned". But note that the value you return MUST have a scope larger than the function. Typically it's a member of the class:
class Example
{
private:
int myarray[10];
public:
// overload the [] operator to return a reference:
int& operator [] (int index)
{
return myarray[index]; // return the appropriate element of 'myarray'
// this is OK because myarray will still be in scope after this function exits
}
};
int main()
{
Example e; // our example object
e[5] = 10; // this is like saying e.myarray[5] = 10
cout << e[5]; // prints 10
}
If I'm really wrong about this, shouldn't my compiler (MSVC10) give me a warning(-Wall) when I do this (with a being a normal variable)?
Because it does not. Even when I make a a reference. *scratching my head*
It diligently warns me when I try this with a pointer(as expected).
I thnk it returns a value of 46.5 (and not an error) because the address at which was stored that value has not been jet modified. (it's still of size double with value stored) and reference is thus correct, otherwise it will return some unpredictable value and access violation.
that means if the value vas not modified yet it could be in few seconds or minutes as program goes furter...
lol I'm geting the point now.
...but I'm not so sure about this part. It is a strange and unusual thing to do (that is, passing out a reference that was passed in).
well it can be usefull maybe to assign the value to some another variable, and then call function back to restore previous value of the reference.
thus you'll have 2 variables : modified and original (for some testig or whatever).
I thnk it returns a value of 46.5 (and not an error) because the address at which was stored that value has not been jet modified. (it's still of size double with value stored) and reference is thus correct, otherwise it will return some unpredictable value and access violation.
that means if the value vas not modified yet it could be in few seconds or minutes as program goes furter...
I'm fully aware of those mechanisms. Just the same as if we used a pointer here.
If you and Athar are right (which I'm gladly willing to admit) I'm wondering, why my compiler does not warn me as it does with pointers to local variables.
In essence the situation is not very much different...
Maybe somone can ask his gcc (or other) for it's opinion/reaction?
well it can be usefull maybe to assign the value to some another variable, and then call function back to restore previous value of the reference.
thus you'll have 2 variables : modified and original (for some testig or whatever).
Not exactly sure what you mean. Why do you have to return a reference or why are you passing by reference in the first place? But in any case, while I'm sure there's some practical use for it, it's still uncommon.
shouldn't my compiler (MSVC10) give me a warning(-Wall) when I do this (with a being a normal variable)?
It probably should. gcc emits a warning in such cases.