C + + const usages use const as much as possible

Source: Internet
Author: User
Tags modifier

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 namespacestd;3 intMain () {4     inta1=3;///Non-const Data5     Const intA2=A1;///Const Data6 7     int* A3 = &a1;///non-const data,non-const Pointer8     Const int* A4 = &a1;///Const DATA,NON-CONST Pointer9     int*ConstA5 = &a1;///non-const data,const PointerTen     int Const*ConstA6 = &a1;///Const DATA,CONST Pointer One     Const int*ConstA7 = &a1;///Const DATA,CONST Pointer A  -     return 0; -}

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.

1   void testmodifyconst (constint  _x) {2      _x=5; /// compilation Error 3 }

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 namespacestd;3 classpoint{4      Public :5Point (int_x): X (_x) {}6 7     voidTestconstfunction (int_x)Const{8 9         ///error, no class member variable can be modified in the const member functionTenx=_x; One  A         ///error, the const member function cannot call a non-ONST member function because non-const member functions can modify member variables - modify_x (_x); -     } the  -     voidModify_x (int_x) { -x=_x; -     } +  -     intx; +};

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 Pointer2     int*a=New int(2);3     returnA;4 }5 6 intMain ()7 {8     Const int*a =Malloca ();9     ///int *b = Malloca (); ///Compile ErrorTen     return 0; One}

(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.

Excerpted from https://www.cnblogs.com/xudong-bupt/p/3509567.html

C + + const usages 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.