Const for C + +

Source: Internet
Author: User

In C + + more powerful than in C!

#define定义常量, just simple text substitution, do not do type detection, error prone.

const int a = 10;

1 //defining a Const constant is the type int by default, and the const-defined symbolic constant has its own data type2 //compile more rigorous detection type Error! Safer than # define. Symbolic constants have addresses that can be pointed to, but cannot be modified by pointers. 3 //The const definition ends with a semicolon, which is treated as a statement. and # define does not write semicolons4 //it can be outside the function and inside the function. Only the scopes are different. 5 //C + + also supports # define definition of symbolic constants, and it is recommended to use a more secure const method6     intx =5;7     #defineT1 x + x8     #defineT2 T1-T19cout <<"T1 ="<< T1 <<"T2 ="<< T2 << Endl;

Explanation: #define只是简单的文本复制! t1=x+x=10 t2=x+x–x+x=x+x=10

Change to const to define symbolic constants: The result is 10 and 0

Const and Pointers

Pointer to constant (note the meaning of the pointer to the constant)

1     intx =5;2     Const int*p;//The pointer p itself is a variable, and *p (pointing to the content) does not necessarily have to point to a constant! 3     //Watch this! It is just that you cannot modify the *p, that is, the content that P points to is not modified by pointer p. 4p = &x;//the value of P itself can be modified5p =NULL;6 7     //*p = ten;//error C3892: "P": cannot assign a value to a constant8x =Ten;//the pointer p points to the content, not the constant, which can be modified. It just can't be modified by the pointer p. 

Constant pointer

1     int 5 ; 2     // pointer p points to a variable 3     int const p = &x; // The pointer p itself is a constant that cannot be modified and must be initialized at the time of definition!  4     ;   OK  5     //p = NULL; // error C3892: "P": cannot assign a value to a constant

Regular pointer to constant

1     intx =5;2     //The pointer p itself is a constant, and the content that P points to is also constant3     Const int*Constp = &x;//must be initialized the first time it is defined4     //p = NULL;//error C3892: "P": cannot assign a value to a constant5     //*p = ten;//error C3892: "P": cannot assign a value to a constant

Note: The use of const protection in function pointer parameters prevents modification of arguments, enhances security, and enhances readability.

Of course there are frequently used references.

Const for C + +

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.