小编Dmi*_*try的帖子

const 引用指的是哪里?

有一个简单的程序

#include <stdio.h>

int counter = 3;

const int& f() {
    return counter++;
}

const int& g() {
    return counter++;
}

const int& h(int w) {
    counter = w;
    return counter;
}

void main()
{
    const int& x = f();
    const int& y = g();
    const int& z = g();
    const int& t = h(123);
    counter = 45678;
    printf("%d %d %d %d", x, y, z, t);
}
Run Code Online (Sandbox Code Playgroud)

为什么它的输出是5 5 5 45678?特别是,为什么x和y是5?如果初始化后它们仍然连接到计数器,为什么它们不是 45679 或类似的东西?

c++ const-reference

-1
推荐指数
1
解决办法
87
查看次数

标签 统计

c++ ×1

const-reference ×1