C ++ is transmitted in the form of three different values

1. There are two kinds of addresses and values ​​in memory,

Just copy the value is passed to the value of the variable parameter

Just pass a reference to the copied address of a variable parameter, the direct address operation in another function

Value of the pointer is in a parameter passed in the argument of the address assigned to the parameter

 

Passing a pointer to the difference between the transmission reference:

(Transmission time when the pointer is transmitted essentially)

1. The reference transfer must be initialized before use, passing a pointer may be initialized at any time

2. When the pointer is passed a pointer can point to other variables, may point to NULL, the reference parameter is transmitted using only the passed address variable

 

Special Uses: When a local function can only return a value to the main function, the actual function needs to return multiple values, then you can use the reference pointer is passed or transferred.

 

★ same point:

● concepts are addresses;

A pointer to a memory, it is referred to the content of the memory address; and references a block of memory is the alias.

★ different points:

● pointer is an entity, but only reference is an alias;

● reference can only be defined when the initialization time, after immutable; pointer variable; references "single-mindedness" pointer "rolling stone";

● no const reference, there is a pointer const, const pointer immutable; (not specifically refer to this int & const a form, and there is const int & a, i.e., the former refers to an alias reference itself may not change, it is, of course, the this need not form, which refers to the value of the reference refers not change)

● null reference can not be empty, the pointer;

● "sizeof reference to" get the size of the variable pointed (object), and "the sizeof pointer" size of a pointer is obtained itself;

● pointer and increment (++) cited operational significance is not the same;

● reference is type-safe, rather than a pointer (quoted more than the pointer type checking)

Released nine original articles · won praise 2 · Views 966

Guess you like

Origin blog.csdn.net/qq_34072169/article/details/103979640