C ++ const usage Summary (welcome to make a brick)

Source: Internet
Author: User

Const is the abbreviation of constant. It is not easy to change. In C ++, const is used to modify built-in type variables, custom objects, member functions, return values, and function parameters. 1. const modifies Common variables. 1 const int a = 7; 2 3 int B = a; // it's right4 5 a = 8; // it's wrong, a is defined as a constant, and a can be assigned to B, but a cannot be assigned again. It is illegal to assign values to a constant because a is considered a constant by the compiler and its value cannot be modified. Next, let's take a look at the following operations: copy the Code 1 2 3 # include <iostream> 4 5 using namespace std; 6 7 int main (void) 8 9 {10 11 const int a = 7; 12 13 int * p = (int *) & a; 14 15 * p = 8; 16 17 cout <a; 18 19 system ("pause "); 20 21 return 0; 22 23} copy the code for const variable a, we take the address of the variable and convert it to the pointer to the int, then use * p = 8; assign a value to the value of variable a, and then output the value of. The following debugging window shows that the value of a is changed to 8, but the output result is still 7. From the results, we can see that the compiler then considers the value of a as 7 defined at the beginning, so the operation on const a will generate the above situation. Therefore, do not assign values to the const variable easily, which produces unexpected behavior. If you do not want the compiler to perceive the above const operations, we can add the volatile keyword Volatile before const to correspond to const, Which is variable-prone and easy to change. So it will not be optimized by the compiler, And the compiler will not change the operations on the variable. Copy code 1 # include <iostream> 2 3 using namespace std; 4 5 int main (void) 6 7 {8 9 volatile const int a = 7; 10 11 int * p = (int *) & a; 12 13 * p = 8; 14 15 cout <a; 16 17 system ("pause "); 18 19 return 0; 20 21} copy the code output result. For example, we expect 8 2. const to modify the pointer variable. There are three cases for modifying pointer variables in const. A: const modifies the content pointed to by the pointer, And the content is not A variable. B: const modifies the pointer, And the pointer is not a variable. C: const modifies the content pointed to by the pointer and pointer. The content pointed to by the pointer and pointer are non-variable. For A: 1 const int * p = 8; // The content 8 pointed to by the pointer cannot be changed. The left value for short, because the const is on the left side of. For B: copy the Code 1 int a = 8; 2 3 int * const p = & a; 4 5 * p = 9; // it's right6 7 int B = 7; 8 9 p = & B; // it's wrong copy code // The memory address pointed to by the const pointer p cannot be changed, but its content can be changed. For short, right-oriented. Because the const is on the right side of. For C: Merge A and B, 1 int a = 8; 2 3 const int * const p = & a; 4 5 // then, the content and memory address pointed to by const p are fixed and cannot be changed. For three cases, A, B, and C, according to the position of the const in the "*" clause, I will summarize three words that are easy to remember: "Left value, right orientation, and const modifiedator ". 3. const parameter transfer and function return value. There are three types of const modifier parameters. A: The const modifier for passing values. In this case, the const modifier is not required, because the function will automatically generate A temporary variable to copy the real parameter value. Copy code 1 # include <iostream> 2 3 using namespace std; 4 5 void Cpf (const int a) 6 7 {8 9 cout <; 10 11 // ++ a; it's wrong, a can't is changed12 13} 14 15 int main (void) 16 17 {18 19 Cpf (8 ); 20 21 system ("pause"); 22 23 return 0; 24 25} copy code B: When the const parameter is a pointer, it can prevent pointer tampering. Copy code 1 # include <iostream> 2 3 using namespace std; 4 5 void Cpf (int * const a) 6 7 {8 9 cout <* a <""; 10 11 * a = 9; 12 13} 14 15 int main (void) 16 17 {18 19 int a = 8; 20 21 Cpf (& ); 22 23 cout <a; // a is 924 25 system ("pause"); 26 27 return 0; 28 29} copy code C: custom type parameter passing, temporary object replication parameters are required. To construct a temporary object, you need to call the constructor, which is a waste of time. Therefore, we adopt the const plus reference transfer method. For general int, double, and other built-in types, we do not use the reference transfer method. Copy code 1 # include <iostream> 2 3 using namespace std; 4 5 class Test 6 7 {8 9 public: 10 11 Test () {} 12 13 Test (int _ m): _ cm (_ m) {} 14 15 int get_cm () const16 17 {18 19 return _ cm; 20 21} 22 23 private: 24 25 int _ cm; 26 27}; 28 29 30 31 void Cmf (const Test & _ tt) 32 33 {34 35 cout <_ tt. get_cm (); 36 37} 38 39 int main (void) 40 41 {42 43 Test t (8); 44 45 Cmf (t ); 46 47 system ("pause"); 48 49 return 0; 50 51} copy the code // enter the result The Return Value of the const modifier Const is divided into three situations. A: const modifies the returned value of the built-in type, which is the same as the return value without modification. Copy code 1 # include <iostream> 2 3 using namespace std; 4 5 const int Cmf () 6 7 {8 9 return 1; 10 11} 12 13 int Cpf () 14 15 {16 17 return 0; 18 19} 20 21 int main (void) 22 23 {24 25 int _ m = Cmf (); 26 27 int _ n = Cpf (); 28 29 30 31 cout <_ m <"" <_ n; 32 33 system ("pause "); 34 35 return 0; 36 37} B: const modifies the User-Defined type as the return value. In this case, the returned value cannot be used as the left value, and cannot be assigned or modified. C: const modifies the returned pointer or reference. Whether to return a pointer to const depends on what we want the user to do. Iv. const modifier class member functions. const modifies a member function to prevent the member function from modifying the value of the called object. If we do not want to modify the value of a called object, all member functions should be declared as const member functions. Note: The const keyword cannot be used together with the static keyword because the static keyword modifies the static member function. The static member function does not include the this pointer, that is, it cannot be instantiated, the const member function must be specific to an instance. The following get_cm () const; function uses the const member function to copy code 1 # include <iostream> 2 3 using namespace std; 4 5 class Test 6 7 {8 9 public: 10 11 Test () {} 12 13 Test (int _ m): _ cm (_ m) {} 14 15 int get_cm () const16 17 {18 19 return _ cm; 20 21} 22 23 private: 24 25 int _ cm; 26 27}; 28 29 30 31 void Cmf (const Test & _ tt) 32 33 {34 35 cout <_ tt. get_cm (); 36 37} 38 39 int main (void) 40 41 {42 43 Test t (8); 44 45 Cmf (t); 46 47 system ("pause "); 48 49 return 0; 50 51} copy the Code. If get_cm () removes the const modifier, the const _ tt passed by Cmf does not change the object value, the compiler also thinks that the function will change the object value, so we try to use all functions that do not need to change the object content as const member functions as required. What if a member function wants to modify a member of an object? In this case, we can use the mutable keyword to modify the Member. mutable means the member is also changeable and easy to change. The member modified by the mutable keyword can be in constant change, as shown in the following example. Copy code 1 # include <iostream> 2 using namespace std; 3 class Test 4 {5 public: 6 Test (int _ m, int _ t): _ cm (_ m ), _ ct (_ t) {} 7 void Kf () const 8 {9 ++ _ cm; // it's wrong10 ++ _ ct; // it's right11} 12 private: 13 int _ cm; 14 mutable int _ ct; 15}; 16 17 int main (void) 18 {19 Test t (8, 7 ); 20 return 0; 21}

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.