What does this mean const int*& var?

后端 未结 4 1229
眼角桃花
眼角桃花 2020-12-23 10:29

I saw someone using this

void methodA(const int*& var);

in one answer, but couldn\'t understand what the argument means.

AFAIK

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 10:41

    Some folks find it easier reading this from right to left. So

    const int*&

    is a reference to a pointer to an integer that is const.

    As you know, references cannot be changed, only what they refer to can be changed. So the reference will refer to just one pointer to an integer that is const. Since the pointer is not const - the integer is const - you can change the pointer to point to a different integer.

    Compare this to

    int* const &

    This is a reference to a constant pointer to an integer. Again the reference is immutable, and in this case it is a reference to a constant pointer. What you can change in this case is the integer value since there was no const either side of the int keyword.

    Just to add confusion, const int and int const are the same. However int const * and int * const are very different. The first is a pointer to a constant integer, so the pointer is mutable. The second is a constant pointer to an integer, so the integer is mutable.

    Hope this helps!

提交回复
热议问题