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

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

Wrong form:

int &z = 12;

Correct form:

int y;
int &r = y;

Question:

5条回答
  •  长情又很酷
    2020-11-28 19:16

    12 is a compile-time constant which can not be changed unlike the data referenced by int&. What you can do is

    const int& z = 12;
    

提交回复
热议问题