Review C + +, it is necessary to some things to make notes, easy to learn later, if there is a problem, welcome to put forward.
First, the two most common composite types should be references and pointers.
A composite type is a type that is defined on a base type base (such as: int).
A reference is a composite type of C + +, and first, unlike pointers, it is not an object, just another object's alias. Also, the reference to the binding must be an object, the reference is not, so the reference that defines the reference is
It's not legal. It is also not possible to define a literal reference. At the same time, the normal reference binding object must be the same as the reference definition type.
Such as:
int &a=ten; // the wrong, must be able to define the literal Double 1.22 ; int &c = b; // error, must be of the same type.
Although, ordinary references cannot be defined as such.
But a const reference can, which means that code like the following is reasonable:
Const &a=; Double 1.22 ; const int &c=b; Const INT 2*b;
Why is const possible? So the code is in the process of compiling it.
For example the third one:
int tem = B; Const int &c=tem;
The system generated a temporary object. In C++primer, the author gives an explanation that the const reference does not allow changes to the object modified by the const, which means that the object that the const reference is bound to cannot be changed, and naturally
Can not change the TEM, but the ordinary reference, we certainly want to change the reference to the object of the binding, or why to use the reference, so, if the rule in the ordinary reference is also tried, it will change the temporary object, which is certainly contradictory.
Pointers are another type of conformance.
A pointer is an object, which is different from a reference, and it binds to the address of another object. Operations on unknown addresses can cause unintended effects. So the pointer does not know where to point, you can point to null first, or the nullptr of the new C + + standard. Null is a preprocessing variable, not a STD space, and can be used directly.
Look at these two pointer definitions: int *p[3] and int (*p) [3], what's the difference?
int *p[3], emphasizing that p[3] is holding an int *, which is an array of holding pointers, memory is three int units.
int (*p) [3], emphasizing that p is a pointer, it points to an int array of size three. This means that p+1 is the first address of an array of the next size three, and can be used as a two-dimensional array of n rows and 3 columns.
A const is a qualifier that represents a constant.
A const-qualified pointer and reference, the equivalent of a protocol, indicates that the value of the pointer or reference will not change, but whether the corresponding value is a constant is not required, as if I only constrained myself, how about your side I don't care.
This means that a normal pointer or reference cannot bind a constant address, and a constant pointer or reference to that can bind a normal address.
A pointer to a constant, such as: const int *a; The address value that the constraint points to should be a constant.
There is also a constant pointer: int const*a; means to constrain itself, the pointer itself is a constant.
Top-level const (top-level Const): is itself a const
Low-leve const (underlying const): Pointer to object is const
The reason for this classification is that, because the underlying const constraint itself does not change the value pointed to, so in the execution of copies or copied out, it is strictly agreed that both sides are the bottom, do not allow the other side to change the value of the object.
The top-level const is constrained by itself, that is, the pointer or other basic data type itself is not changed, which means that the copy-in and copy-out does not need to be a top-level const, because such an operation will not change himself.
C + + Learning notes-composite type, Const.