返回 type of const int &

Return type of const int &

本文关键字:int const of type 返回      更新时间:2023-10-16
const int z1 = 5;
const int & giveMeNumber1(){
    return z1;
}
int z2 = 6;
const int & giveMeNumber2(){
    return z2;
}
int main(){
    int y = giveMeNumber1();
}
  1. 两个函数的返回类型似乎都是const int。我说的对吗?如果是这样,为什么我可以分配int y = giveMeNumber1(); ?

  2. 为什么这些函数没有分别返回z1z2的实际地址?当我写:

    int x = 3; cout << &x << endl;
    

    输出x变量的地址,因此:

    cout << giveMeNumber1() << endl;
    

    应该也打印地址(const int & return type),但是它打印的是5

哇,这些误解。好的,让我们看看我们能做些什么。

两个函数的返回类型似乎都是const int。我说的对吗?

。这两个函数的返回类型都是const int&。参考const int .

如果是这样,为什么我能分配int y = giveMeNumber1(); ?

因为你要复制到y ?您可以将const对象复制到非const对象中。一个const的东西不能被修改,但是复制一些东西不会修改它。

为什么这些函数没有分别返回z1z2的实际地址?

因为他们不应该。它们返回对z1z2的引用。

cout << giveMeNumber1() << endl;

也应该打印地址(const int &返回类型)

一派胡言。你在用哪本c++书?返回类型const int&(至少这次您做对了)表示引用。引用不是指针,也不是"地址"。我不知道你从哪里得到的概念,cout << giveMeNumber1()会打印任何地址,但& help;它不会。

要了解更多信息,请重新阅读您的c++书籍& help;这一次,封面到封面!

    这两个函数的返回类型似乎都是const int。我正确吗?如果是这样,为什么我能够分配int y = giveMeNumber1();?

不,你错了。这些函数的返回类型是const int &。注意末尾的&符号&。它是对int的const引用。

giveMeNumber1()返回一个左值为整型的const int &(即z1)。不能通过const-reference修改被引用的对象(这就是这里const背后的含义)。但是你可以复制这个值。因此,实际情况是,引用的值被复制到y

  • 为什么这些函数没有分别返回z1和z2的实际地址?
  • 当我写:

    int x = 3; cout << &x << endl;

    打印x变量的地址,因此:

    cout << giveMeNumber1() << endl;

    应该也打印地址(const int & return type),但是它打印的是5。

    同样,这些函数返回一个指向int的const引用。它们不返回地址。

    似乎你对指针/地址和引用的&的使用感到困惑。

    指针将地址存储到另一个内存位置。所以你可以说它们"指向一个对象"。

    引用有点像指针,但它们的行为就像对象本身。你只能用左值初始化它们,不能用像int& ref = 1这样的文字值初始化引用。

    可以对原始对象做的任何事情都可以对引用做。但是你不能重新分配一个引用,一旦它引用了一个对象,你就不能改变它,尽管有一些标准的包装器可以用来克服这个问题。

    像使用对象本身一样使用引用包括获取其地址。因为你的函数返回一个引用,你也可以得到被引用对象的地址。您可以像这样编写cout行,

    cout << &giveMeNumber1() << endl;  // The & here gets the address of the left value
    // returned by the function.
    

    如果你想返回变量的地址,那么你可以这样写函数,

    // The returned type is a "pointer to const int", note the *
    const int* giveMeNumber1() {
        return &z1;
    }
    

    但是,不要将函数的返回值称为"返回其地址",而应称为"返回指针"。这样,您的原始cout行将直接打印地址

    cout << giveMeNumber1() << endl;
    

    如果你想引用指向的值,你可以写,

    cout << *giveMeNumber1() << endl;
    

    这里有一个简短、密集的例子来帮助你区分引用和指针,

    int  x = 16;
    int* p = &x; // Pointer p initialized with the address of x
    int& r =  x; // r is a reference to x
    const int& cr = x; // const reference to x
    std::cout <<  x << std::endl; // x's value = 16
    std::cout << &x << std::endl; // x's address
    std::cout <<  p << std::endl; // p's value = address of x
    std::cout << *p << std::endl; // int value that's in the address pointed to by p == x == 16
    std::cout << &p << std::endl; // p´s address (p is a variable as well)
    std::cout <<  r << std::endl; // x's value = 16
    std::cout << &r << std::endl; // x's address (you can't get the address of the reference itself)
    r  = 2;  // r is a reference of x, so now x = 2
    cr = 2; // This won't do, you can't assign to a const reference
    int y = r;  // Ok, copying the value in x, 2, to y
    int z = cr; // Ok, copying 2 to z
    

    编辑:编辑以正确地解决OP的问题。

    返回z1作为引用

    return &z1;
    

    实际上,您返回的是由变量z1中包含的值表示的地址。不是内存中z1的地址。

    这个值可以很好地放入int中