"Go" C + + const usage Use const as much as possible

Source: Internet
Author: User
Tags modifier

Http://www.cnblogs.com/xudong-bupt/p/3509567.html

C + + const allows a semantic constraint to be specified, and the compiler enforces this constraint, allowing the programmer to tell the compiler that a value is unchanged. If there is a value in programming that does not change, you should explicitly use const so that you can get help from the compiler.

1.const decorated member variables

1 #include <iostream> 2 using namespace std; 3 int Main () {4     int a1=3;   Non-const Data 5     const int A2=A1;    Const Data 6  7     int * a3 = &a1;   Non-const data,non-const pointer 8     const int * a4 = &a1;   Const DATA,NON-CONST Pointer 9     int * Const A5 = &a1;   Non-const data,const pointer10     int const * CONST A6 = &a1;   Const DATA,CONST POINTER11     const int * Const A7 = &a1;   Const DATA,CONST Pointer12     return 0;14}

When const modifies pointer variables:

(1) There is only one const, if the const is on the left side, indicating that the pointer data is a constant, you cannot modify the data by dereferencing it; The pointer itself is a variable and can point to other memory units.

(2) There is only one const, if the const is on the right, indicating that the pointer itself is a constant and cannot point to another memory address; the data that the pointer refers to can be modified by a dereference.

(3) Two const,* each, indicating that the pointer and pointer data cannot be modified.

2.const modifier function Parameters

The passed arguments cannot be changed within the function, as are the properties of the modified variables.

void Testmodifyconst (const int _x) {     _x=5; Compilation Error}

3.const Modifier member functions

(1) Const-Modified member function cannot modify any member variable (except mutable-Modified variable )

(2) The const member function cannot call a non-ONST member function because non-const member functions can modify member variables

1 #include <iostream> 2 using namespace std; 3 class point{4     public:5 point     (int _x): X (_x) {} 6  7     void testconstfunction (int _x) const{8  9         / Error, in the const member function, you cannot modify any of the class member variables of the X=_X;11////         error, the const member function cannot call a non-onst member function, because non-const member functions can modify member variables         Modify_x (_x);     }15     void modify_x (int _x) {+         x=_x;18     }19     int x;21};

4.const modifier function return value

(1) Pointer passing

If you return a const data,non-const pointer, the return value must also be assigned to the const data,non-const pointer. The data that the pointer points to is a constant that cannot be modified.

1 Const int * Malloca () {  ///const data,non-const pointer 2     int *a=new int (2); 3     return A; 4} 5  6 int Main () 7 {8     const int *a = Malloca (); 9     ///int *b = Malloca ();  Compilation Error     0;11}

(2) Value transfer

If the function return value is in "value passing", the const modifier has no value because the function copies the return value into an external temporary storage unit. So, for value passing, the addition of Const does not make much sense.

So:

Do not write the function int GetInt (void) as a const int GetInt (void).
Do not write function a geta (void) as const a geta (void), where a is a user-defined data type.

  To use const as much as possible in programming, you get the help of a compiler to write robust code.

"Go" C + + const usage Use const as much as possible

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.