小编mar*_*rde的帖子

如何在c ++中解释"const int*const&variable"

当一个将两个变量别名为

int a;
const int &b = a;
Run Code Online (Sandbox Code Playgroud)

这两个变量实际上是相同的,因此应用于变量的任何更改a也会应用于变量b.但是,当使用指针完成相同的技巧时,它似乎无法以相同的方式工作,如以下程序所示:

#include <iostream>
int main(void) {

    int *a = (int*) 0x1;
    const int *const &b = a;// Now b should be an alias to a.
    a = (int*) 0x2;// This should change b to 0x2.
    std::cout << b << "\n";// Outputs 0x1 instead of the expected value of 0x2.

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在变量a似乎不是变量的别名b,但为什么呢?

c++ pointers const reference

4
推荐指数
2
解决办法
503
查看次数

标签 统计

c++ ×1

const ×1

pointers ×1

reference ×1