C + + Primer second Chapter reference Pointer Const qualifier

Source: Internet
Author: User

1. References:

A different name is given to the object, the reference type refers to another type, and the reference type is defined by writing the declarator as &d, where D is the declared variable name (the declarator is the variable name).

Ps:1. The error message indicated by compiling the picture shows that the reference must be initialized;

2. The reference is not an object, but instead it is just another name for an already existing object;

2. Pointers:

Like references, pointers also implement indirect access to other objects, but the pointer itself is an object that allows the pointer to be assigned and copied, and can point to several different objects in its lifetime (the reference can only point to one initialization).

The pointer does not need to be defined when the initial value is assigned, as with other built-in types, a pointer defined within a block scope will have an indeterminate number if it is not initialized.

PS: The pointer holds the address of an object, and the value of the pointer is the address;

1     int*P1,*P2;//P1 and P2 are pointers to int type objects;2     DoubleP3,*P4;//P4 is a pointer to a double type object, and P3 is a double object;3     inti = A;4     int*P1 = &i;//P1 stores the address of the variable I, or P1 points to I;5*P1 =2;//correctly, the object referred to by the pointer can be assigned by P as the variable i;6P2 = &i;//correct, the same is the int* type;7*P2 = &i;//error, type cannot be converted, &i is int* type, *p2 is int type;8P4 =0;//correct, since 0 has no data type;

3.const qualifier:

A const-decorated data type is a constant type, and the value of a variable or object of a constant type cannot be updated.

Its main functions are:

The

(1) can define const constants, which are immutable.  
For example: const int max=100; max++ will produce errors;  
(2) facilitates type checking, which allows the compiler to understand more about the content of the process and eliminates some of the pitfalls.
For example: void f (const int i) {...} The compiler will know that I is a constant and is not allowed to be modified;  
(3) can avoid the ambiguity of the number appears, the same can be easily adjusted and modified parameters. As with the definition of a macro, you can do it without changing it.
such as (1), if you want to modify the content of Max, only need: const int max=you want;  
(4) can protect the modified things, prevent accidental modification, enhance the robustness of the program. Or the above example, if I is modified in the function body, the compiler will error;  
For example: void f (const int i) {i=10;//error!}  
(5) can save space and avoid unnecessary memory allocations. For example:  
#define PI 3.14159//Macro constants  
Const double pi=3.14159;//The PI is not put into ram ...  
Double I=pi; Allocate memory for PI at this time, no longer assigned!  
double i=pi;//macro substitution during compilation, allocation of memory  
double j=pi;//no memory allocation  
double j=pi;//macro substitution, memory allocation again!  
Const definition constants from a compilation point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the constant in the program runs only one copy, and # The constants defined by define have several copies in memory.  
(6) improves efficiency.   The
compiler usually does not allocate storage space for ordinary const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the operation of memory and read memories, making it highly efficient.

Ps:const-Qualified object rules (left-right-pointing: the position of Const and *), also called the top-level const and the underlying const.

What is the immutable content of a const qualifier? 1) const in front const int nvalue;//nvalue is const const CHAR *pcontent; *pcontent is const, pcontent variable const char* const pcontent; Pcontent and *pcontent are both const.

C + + Primer second Chapter reference Pointer Const qualifier

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.