c ++ references and pointers in relations

Format references: type & ref = var;

the difference:

1. reference only aliases variables, do not open up new space, with the original variables using the same memory unit. Pointer is a new variable, have their own storage space.

2. The reference must be initialized in the declaration, the pointer can be initialized at any time.

3. The reference can not be empty, you must always refer to an object. Pointer can be empty, do not point to any place.

4. Reference Once initialized, it can not reference other objects. If the pointer is not modified by const, can be re-directed to a different variable.

5. If the reference is modified const, constants can be assigned at initialization. In addition to the pointer 0, no time can be assigned directly without conversion constants.

6. a statement with reference to & As identifier, like a variable directly use. * Pointers as identification, but also with the use of indirect access *.

When the operation is applied to 7.sizeof referenced test that the width of the referenced object. As used pointer, the pointer width of the test itself.

Common:

1. The nature also cited pointer, but the compiler has been converted at compile time.

2. pointers and references are indirect references to other objects. Or indirectly via the former is the address pointed object, which is a direct access to the object referenced by an alias.

Guess you like

Origin blog.csdn.net/m0_37772527/article/details/88967423