Constant type: A type with a const description of the type modifier, a variable of a constant type, or the value of an object member that is immutable during a program run.
3.10.1 frequently quoted
If a const adornment is used in the description reference, the referenced reference is a constant reference. If you use a constant reference to make a formal parameter, you do not produce an argument
The unwanted changes to the. The frequently cited forms are as follows:
Const type & reference name;
For example: int a=5;
const INT &b=a;
where b is a constant reference, and the object it references does not allow changes. If it appears: b=12//IS illegal
In practice, a constant reference is often used to make a formal parameter, which is called a long-form parameter.
Example3.39arguments that are often referenced as functions*/#include<iostream>using namespacestd;intAddConst int&,Const int&);//declares that the add parameter is a common referenceintMain () {intA= -; intb= -; cout<<a<<"+"<<b<<"="<<add (b) <<Endl; A= the;//arguments can be changed outside of the Add functionb = -;//arguments can be changed outside of the Add functioncout<<a<<"+"<<b<<"="<<add (b) <<Endl; return 0; }intAddConst int&i,Const int&J)//often referenced as a function parameter{ //i=i+20; //changing the value of I is not allowed returni+J;} /*program Run Results: 20+30=50 15+50=65 Description: Because the Add function two parameters are defined as constant reference, so in the function can not change the values of I and J, if you change their Value, compilation will error, such as the function executed in this program add annotated statement "I=I+20;", a compilation error will occur. Therefore, it is possible to avoid the change of the actual parameter and ensure the security of the data by using the constant reference as the formal parameter. */
3.10.2 Constant Object
If the object is described with a const modifier, the object being described is a constant object. The data member value of a constant object is
cannot be changed throughout the lifetime of an object. The usual object is described as follows:
Class name Const & object name [(parameter table)]
or const class name & object name [(parameter table)]
The object must be initialized when it is defined and cannot be updated.
Cases3.40comparison between a very object and a constant object*/#include<iostream>using namespacestd;classsample{ Public: Sample (intMintN) {i=m; J=N; } intJ; voidSetValueintx) {i=x; } voidShow () {cout<<"i="<<i<<Endl; cout<<"j="<<j<<Endl; } Private: inti; };intMain () {Sample s (Ten, -);//Object S is a normal object, not a regular objectS.setvalue ( -); S.J= +; S.show (); //The running result is i=30 j=40/*Sample const S (10,20);//object S is a constant object S.setvalue (30); Run error, C + + does not allow indirect changes to the value of I s.j=40; Run error, C + + does not allow the value of the set Change J S.show (); Run error, C + + does not allow regular objects to call ordinary*/return 0;}
3.10.3 Regular Object Members
1. Regular data members
A data member of a class can be a constant or a regular reference, and a data member that uses a const description is called a constant data member. If the
When a constant data member is described in a class, the constructor can only be used by the member initialization list for the data member
Initialized, and no other person can assign a value to the member.
Example 3. Ause of regular data members*/#include<iostream>using namespacestd;classdate{ Public: Date (intYintMintd); voidShow (); Private: Const intYear//Regular data members Const intMonth//Regular data members Const intDay//Regular data members};D ate::D ate (intYintMintD): Year (y), month (m), day (d) {}//Initializes a data member with a member initialization listvoiddate::show () {cout<<year<<"."; cout<<month<<"."; cout<<day<<Endl;} intMain () {Date date1 ( -,5,7); Date Date2 ( -,5,8); Date1.show (); Date2.show (); return 0;}/*The result is: 2015.5.7 2015.5.8 The program defines the following 3 constant data members: const int year; Constant data member const INT month; Constant data member const int day; Regular data members where year, month, day are data members of type int. It is important to note that the format of the constructor is as follows: Date::D ate (int y,int m,int D): Year (y), month (m), day (d) {} where the colon is followed by a member initialization list that contains 3 initializers. This is because year, month, and day are constant data members, and C + + specifies that regular data members can be initialized only through the constructor's initialization list. It is illegal to assign an initial value directly to a data member in the function body using an assignment statement. The constructor is wrong in the following form: Date::D ate (int y,int m,int d) {year = y; Illegal month = m; Illegal day = D; Illegal} Once the constant data member of an object is initialized, the value of the data member cannot be changed, but the value of the data member in different objects can be different (given when defining the object) such as Date date1 (2015,5,7); Date Date2 (2015,5,8);*/
2. Regular member functions
The member functions that use the const description in a class are constant member functions, and the description format of the constant member function is as follows:
Type specifier function name (parameter table) const;
A const is a component of a function type, so you use the keyword const when declaring a function and defining a function.
You do not have to add a const when calling.
Example 3. theuse of constant member functions*/#include<iostream>using namespacestd;classdate{ Public: Date (intYintMintd); voidShow ();//declaring a normal member function show () voidShow ()Const;//declaring a regular member function show () Private: Const intYear//Regular data members Const intMonth//Regular data members Const intDay//Regular data members/*int year; Normal data member int month; Ordinary data member int day; Ordinary data members*/};D ate::D ate (intYintMintD): Year (y), month (m), day (d) {}//define constructors, with member initialization lists,//initializing a data membervoidDate::show ()//define a normal member function show (){cout<<"Date::show1 ()"<<Endl; cout<<year<<"."; cout<<month<<"."; cout<<day<<Endl;} voidDate::show ()Const //define a constant member function show (){cout<<"Date::show2 ()"<<Endl; cout<<year<<"."; cout<<month<<"."; cout<<day<<Endl;} intMain () {Date date1 ( -,5,7);//defining ordinary Objects Date1Date1.show ();//call the normal member function show ConstDate Date2 ( -,5,8);//defining a Constant object Date2Date2.show ();//calling the constant member function show return 0;}/*The result of the program operation is as follows: Date::show1 () 2015.4.7 date::show2 () 2015.4.8 in this program, class date describes the two member functions of the same name show, one is a normal member function, the other is a regular member function, they are overloaded. Visible, the keyword const can be used to differentiate overloaded functions.
Description
1, the regular member function can access the regular data members, you can also access ordinary data members. A constant data member can be accessed by a regular member function,
can also be accessed by ordinary member functions.
2. If an object is described as a constant object, the object can only be called by its regular member function, not the normal member
function. The constant member function is the only external interface of a constant object, at which time C + + is syntactically protected from the constant object.
Table 3.2 Comparison of access characteristics between common member functions and ordinary member functions
----------------------------------------------------------------------------------------------------------- ----
Data Members | Ordinary member functions | Constant member function
----------------------------------------------------------------------------------------------------------- ----
Normal data members can access or change the value can be accessed, but can not change the value
regular data members can access, but can not change the value can be accessed, but can not change the value
data members of a constant object do not allow access and change the value can be accessed, but can not change the value
3. The constant member function cannot update the data member of the object, nor can it call the ordinary member function in the class, which guarantees the constant member
the value of the data member is never updated in the function.
C + +: Constant Type const