return type in c++

浪子不回头ぞ 提交于 2019-12-01 11:44:55

问题


#include<iostream>

int & fun();
int main()
{
    int p = fun();
    std::cout << p;
    return 0;
}

int & fun()
{
    int a=10;
    return a;
}

Why is this program not giving error at line no.6 as "invalid conversion from int* to int", as it happens in case we do like this?

 int x = 9;
 int a = &x;

回答1:


int& is a type; it means "a reference to int."

&x is an expression; it means "take the address of x." The unary & operator is the address operator. It takes the address of its argument. If x is an int, then the type of &x is "a pointer to int" (that is, int*).

int& and int* are different types. References and pointers are the same in many respects; namely, they both refer to objects, but they are quite different in how they are used. For one thing, a reference implicitly refers to an object and no indirection is needed to get to the referenced object. Explicit indirection (using * or ->) is needed to get the object referenced by a pointer.

These two uses of the & are completely different. They aren't the only uses either: for example, there is also the binary & operator that performs the bitwise and operation.


Note also that your function fun is incorrect because you return a reference to a local variable. Once the function returns, a is destroyed and ceases to exist so you can never use the reference that is returned from the function. If you do use it, e.g. by assigning the result of fun() to p as you do, the behavior is undefined.

When returning a reference from a function you must be certain that the object to which the reference refers will exist after the function returns.




回答2:


Why is this program not giving error at line no.5 as "invalid conversion from int* to int", as it happens in case we do like this?

That's because you are trying to return the variable by reference and not by address. However your code invokes Undefined Behaviour because returning a reference to a local variable and then using the result is UB.




回答3:


Because in one case its a pointer and in the other a reference:

int a=&x means set a to the address of x - wrong

int &p=fun() means set p to a reference to an int - ok




回答4:


Functions in C++ are not same as macros i.e. when you qrite int p = fun() it doesn't become int p = &a; (I guess that is what you are expecting from your question). What you are doing is returning a reference from the function f. You are no where taking address of any variable. BTW, the above code will invoke undfeined behavior as you are returning a reference to the local variable.




回答5:


You're not returning an int *, you're retuning an int &. That is, you're returning a reference to an integer, not a pointer. That reference can decay into an int.




回答6:


Those are two different things, although they both use the ampersand symbol. In your first example, you are returning a reference to an int, which is assignable to an int. In your second example, you are trying to assign the address of x (pointer) to an int, which is illegal.



来源:https://stackoverflow.com/questions/5932239/return-type-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!