简体   繁体   English

在C ++中返回类型

[英]return type in c++

#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? 为什么该程序在第6行没有给出“从int *到int的无效转换”的错误,因为在我们这样做时会发生这种情况?

 int x = 9;
 int a = &x;

int& is a type; int&是一种类型; it means "a reference to int ." 它的意思是“对int的引用”。

&x is an expression; &x是一个表达式; it means "take the address of x ." 它的意思是“取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* ). 如果x是一个int ,则&x的类型是“一个指向int的指针”(即int* )。

int& and int* are different types. int&int*是不同的类型。 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. 还要注意,您的函数fun不正确,因为您返回了对局部变量的引用。 Once the function returns, a is destroyed and ceases to exist so you can never use the reference that is returned from the function. 函数返回后, a被销毁并不再存在,因此您永远无法使用从函数返回的引用。 If you do use it, eg by assigning the result of fun() to p as you do, the behavior is undefined. 如果您确实使用它,例如,像您一样将fun()的结果分配给p ,则行为是不确定的。

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. 从函数返回引用时,必须确保在函数返回后该引用所引用的对象将存在。

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? 为什么该程序在第5行没有给出“从int *到int的无效转换”的错误,因为在我们这样做时会发生这种情况?

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. 但是,您的代码将调用Undefined Behavior,因为返回对局部变量的引用然后使用结果是UB。

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 a =&x表示将a设置为x的地址-错误

int &p=fun() means set p to a reference to an int - ok int&p = fun()意味着将p设置为对int的引用-好的

Functions in C++ are not same as macros ie when you qrite int p = fun() it doesn't become int p = &a; C ++中的函数与宏不同,即,当您对int p = fun() qrite时,它不会变为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 . 您正在做的是从函数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. 顺便说一句,当您返回对局部变量的引用时,以上代码将调用不可侵犯的行为。

You're not returning an int * , you're retuning an int & . 您不返回int * ,而是重新调整int & That is, you're returning a reference to an integer, not a pointer. 也就是说,您将返回对整数的引用,而不是指针。 That reference can decay into an int . 该引用可能会衰减为一个int

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. 在第一个示例中,您将返回对int的引用 ,该引用可分配给int。 In your second example, you are trying to assign the address of x (pointer) to an int, which is illegal. 在第二个示例中,您试图将x(指针)的地址分配给int,这是非法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM