Why are pointers to a reference illegal in C++?

前端 未结 5 1755
悲&欢浪女
悲&欢浪女 2020-12-04 14:50

As the title itself mentions - why are pointer to a reference illegal, while the reverse is legal in C++?

5条回答
  •  忘掉有多难
    2020-12-04 14:57

    The high-level concept that references implement is just another name for an existing object. You can have a pointer to an object (or function), but you can't have a pointer to an object's name. For this very reason, the idea of a pointer to a reference makes no sense. In other words, references are immaterial, in general case they simply do not exist in memory. They don't exist as something that can be pointed to.

    It is true that in many cases in practice references do occupy memory (and are implemented as pointers in disguise). But that just an implementation detail specific to some particular contexts. In general case references do not occupy memory, as is explicitly stated in the language specification which immediately follows from the language specification.

提交回复
热议问题