1. 指针和引用有什么区别?
- A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.
- A pointer can point to NULL while reference can never point to NULL
- You can't take the address of a reference like you can with pointers
- There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).
To clarify a misconception:The C++ standard is very careful to avoid dictating how a compiler must implement references, but every C++ compiler implements references as pointers. That is, a declaration such as:
int &ri = i;
allocates the same amount of storage as a pointer, and places the address of i into that storage.
本文详细解释了C++中指针与引用的主要区别,包括:指针可以被重新赋值而引用不能;指针可以指向NULL而引用不可以;不能取引用的地址等特性。此外还澄清了一个常见的误解,即虽然标准没有规定引用的具体实现方式,但所有C++编译器实际上都是通过指针来实现引用的。

6451

被折叠的 条评论
为什么被折叠?



