error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

前端 未结 5 1566
盖世英雄少女心
盖世英雄少女心 2020-11-28 18:42

Wrong form:

int &z = 12;

Correct form:

int y;
int &r = y;

Question:

5条回答
  •  时光说笑
    2020-11-28 19:27

    References are "hidden pointers" (non-null) to things which can change (lvalues). You cannot define them to a constant. It should be a "variable" thing.

    EDIT::

    I am thinking of

    int &x = y;
    

    as almost equivalent of

    int* __px = &y;
    #define x (*__px)
    

    where __px is a fresh name, and the #define x works only inside the block containing the declaration of x reference.

提交回复
热议问题