C + + 's const

Source: Internet
Author: User

Const in C + + 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. The most common use of const is as an array of bounds and switch sub-condition labels (also can be substituted with enumerators), categorized as follows:

Constant variable: const type descriptor variable name

Constant reference: const type specifier & reference name

Constant object: Class name const object name

Constant member function: Class Name:: Fun (formal parameter) const

Constant group: type specifier const array name [size]

Constant pointer: const type specifier * Pointer name, type specifier * Const pointer name

It should be noted that the const only works on the left side of it, and the only exception is that the const itself is the leftmost modifier, so it works on the right side of the thing. In a constant variable (const type specifier variable name), constant reference (const type specifier & reference name), constant object (class name const object name), const group (type specifier const array name [size]), and "type specifier" or "Class name" (In fact, the class name is a self- The position of the defined type specifier can be interchanged. Such as:
const int a=5; const a=5 with int; Equivalent
Class name const object Name equals const class name Object name

I. Constants and pointers

1#include <stdio.h>2 intMain ()3 {4     intK = -;5     intQ = the;6     Const int*PA = &K;7     int*ConstPB = &K;8      9printf"PA =%x *pa =%d | pb =%x *PB =%d\n", pa,*pa,pb,*pb);Ten     //pa = PB = &k *pa = *PB = One //*PA =;//Compile Error A*PB = About; -printf"PA =%x *pa =%d | pb =%x *PB =%d\n", pa,*pa,pb,*pb); -     //pa = PB = &k *pa = *PB = the //PB = &q;//Compile Error -PA = &Q; -printf"PA =%x *pa =%d | pb =%x *PB =%d\n", pa,*pa,pb,*pb); -     //PA = &q PB = &k *pa = *PB = +     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.

Ii. Constants and references

The relationship between constants and references is slightly simpler. Because a reference is an alias of a variable, it is itself a constant. That is, you can no longer have a reference to be an alias for another variable, so they are left with a variable representation of the memory area. That

1 int Ten ; 2 Const int& ri = i; // correct: Indicates that the reference cannot be used to modify the contents of the corresponding memory.     3 int const RCI = i; // Error! Can't write like this. 

Thus, if we do not want the caller of the function to change the value of the parameter. The most reliable method should be to use a reference. There is a compilation error in the following operation:

1 voidFuncConst int&i)2 {    3i = -;//Compile Error! You cannot change the area of memory that it represents by I. 4 }5 intMain ()6 {    7         inti =Ten; 8 func (i); 9         return 0;Ten}

Three, constant function

(1) Const-Modified member function cannot modify any member variable other than 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#include <stdio.h>3 using namespacestd;4  5 classpoint{6      Public :7Point (int_x,int_y): X (_x), Y (_y) {}8     voidTestconstfunction (int_x,int_y)Const9     {      Ten //x=_x;//compilation error, no class member variable can be modified in the const member function One //modify_x (_x);//a const member function cannot call a non-const member function because a non-const member function can modify a member variable if a compilation error Ay =_y; -     } -     voidModify_x (int_x) { thex=_x; -     } -     intx; -mutableinty; + }; -   + intMain () A { atPoint pt (3,5); -printf"x =%d y =%d\n", PT.X,PT.Y);//x = 3 y = 5 -Pt.testconstfunction (5,7); -printf"x =%d y =%d\n", PT.X,PT.Y);//x = 3 y = 7 -     return 0; -}

Four, constant return value

Many times, we return an address or reference in our function. Call this to get the return address or reference to modify the object that you are pointing to or representing. This time if we don't want this function to call this to modify the returned content, you should return a constant. That

1 Const int // the constants are returned, so the const int A=FF () must be called so

C + + 's const

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.