Summary of the use of the Const keyword in C + +

Source: Internet
Author: User
Tags modifier

The const keyword is used in C + + to modify constants, which are summarized in two ways: variables and member functions.

Variables: const can modify ordinary variables, pointers (arrays), and structs.

1.const modification of common variables is the simplest case. This is used to create a read-only variable in the program, similar to the C # define macro definition, but a const-declared variable can specify a type, so it is advocated in C + + to declare read-only variables using the const keyword.

Const int  ; /* Sample Code 1 */

Example code 1 indicates that an int variable is defined month, and the program can use the variable, but the month variable cannot be re-assigned, such as:

int 3           6; /* ERROR */

2. The following is the case of the const modifier pointer. Such a situation is more common in the pointer/array as a function parameter, in order to prevent the call to the function inadvertently or incorrectly modified the original pointer data, often before the pointer parameter to add the const keyword, such as:

void  ShowMsg (constchar *strmsg); /* Sample Code 2 */

Example code 2 declares a function showmsg with a return value of void, assuming that the function is to display the contents of a string, we do not want the function to modify the contents of the original string when used, so you can add the Const keyword before the argument, so that the If the code in the function code modifies the strmsg string, the program compiles an error.

Below, the combination of const and pointers is described in four different situations:

 /*   example code 3  */ int  a = 10   const          int  *PB = &A; /* 1 */ int  const  *PB = &a; /* 2 */  int  * const  PB = &a; /* 3 */  const    int  * const  PB = &a; /* 4 */ 

Looking at the four scenarios in example code 3, we can remember that the const is on the left side of the asterisk to indicate that the pointer to the variable is a constant value, as in the above 1, 22 cases, the pointer variable points to a constant value, you cannot use the pointer variable PB to modify the value, in other words, * The value of PB is const non-modifiable, that is, *PB = 11; (But a = 11 is legal, int b = 11;PB = &b is also legal). Note: You can assign the address of a const variable to a const pointer (such as example 3), but you cannot assign the address of a const variable to a non-const pointer. The following code is illegal :

/* ERROR code example */
Constintten; int *p = &a;

The const on the right side of the asterisk represents a constant pointer to which the address cannot be modified. 3 indicates that PB can only point to &a addresses, but can use *PB = 11来 to change the value of a. 4 is the combination of the above two conditions.

3. The use of the Const modifier function typically appears in the member function, as in the following form:

/* Sample CODE 4 */
Class demo{ Private: ...
Public
...
void ShowMsg () const; /* Function declaration */
};

A function declared as in example code 4 is called a const member function, and the function does not modify the class object, that is, the private member variables of the class are not altered.

Summary of the use of the Const keyword in C + +

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.