How "const int * const & variable" is interpreted in C ++

When one alias uses two variables as

int a;
const int &b = a;

the two variables are virtually the same, so any change applied to the variable aalso applies to the variable b. However, when the same trick is performed with pointers, it does not work as shown in the following program:

#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;
}

Now a variable ais not an alias of a variable in the end b, but why?

+4
source share
2 answers

const int *const & const const int. ( .) , const int *, int * (.. a). . const int *const &b = a; * ( const int *, a), ; a, b a.

. 1- const int; const , , (int * vs. const int *). const ( ), , .. int * const &.


* b.

+12

const int * const & b const int. int * const & b

. https://cdecl.org/

+5

Source: https://habr.com/ru/post/1691715/


All Articles