Search code examples
c++pointerspass-by-reference

Sequence of a pointer reference type


How to pass a pointer variable as a reference parameter?

I have an additional question about this question.

The answerer in the question is using int*& as the parameter data type, but I don't understand the sequence of the pointer and reference data type.

In my opinion, int&* looks more practical to me, so I tried to compile with that, but it wasn't allowed.

I wanna understand clearly about this logic.

I thought int*& a like int*(int& a), so it's like a pointer of a reference, but apparently, it was just right the opposite.

Why can't I use int&* as the meaning of a reference of a pointer?


Solution

  • C++ types are name-centered. int*&foo is how you should think of it.

    First, we have &foo -- foo is a reference. A reference to what? *&foo -- a pointer. A pointer to what? int*&foo an integer.

    The original C type rules where even intended to "demonstrate how they are used". So int *foo is short hand for int = *foo being a valid expression, or *foo makes an int. The & doesn't quite work that way.