您的位置:首页 > 编程语言 > C语言/C++

全面整理的C++面试题(II)

2013-01-28 13:55 375 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: