What is the difference between references and normal variable handles in C++?

后端 未结 8 962
一生所求
一生所求 2021-01-22 23:33

If C++, if I write:

int i = 0;
int& r = i;

then are i and r exactly equivalent?

8条回答
  •  误落风尘
    2021-01-22 23:51

    Yep - a reference should be thought of as an alias for a variable, which is why you can't reassign them like you can reassign pointers (and also means that, even in the case of a non-optimizing compiler, you won't take up any additional storage space).

    When used outside of function arguments, references are mostly useful to serve as shorthands for very->deeply->nested.structures->and.fields :)

提交回复
热议问题