Const keyword summary in C ++

Source: Internet
Author: User

When you call a program in the afternoon, you will encounter an error in the compilation of error: passing '$ class_name' as 'this 'argument of' $ class_function' discards qualifiers,

The error indicates that when the this pointer is passed into class_function as a parameter, the qualifier is canceled.

Google, this function requires const modification, and I missed it, So I reported this compilation error.

 

By the way, we will summarize the usage of const keywords in C ++.

 

Const: the abbreviation of constant. It must have been explained by the teachers in the first C/C ++ course. "const is a constant, avoid reducing the magic number in the program )". In fact, this is the biggest misunderstanding of const, so that you have seen const for many years and think of the constant variables defined in the first few lines of the file.

 

The usage of const in C ++ is summarized

Const variable Modification

Cosnt-pointer Modification

Const's Function Modification

 

1. const variable Modification

Const int MAX_SIZE = 1024;

This is the most common usage. It is okay for everyone. const needs to be initialized during definition.

Pay attention to the following two points:

A) the const variable is a local variable by default. If global access is required, extern must be displayed.

B) const int MAX_SIZE = 1024 and # define MAX_SIZE1024 seem to have done the same thing, but they are totally different.

# Define is used as the macro definition to completely replace the text, while const MAX_SIZE is used as the whole variable.

The following code makes it clearer:

Const int MAX_SIZE = 1 <10; // # define MAX_SIZE 1 <10

Cout <MAX_SIZE <std;

 

Ii. const's pointer Modification

Const int * a = & B; (1)

Int const * a = & B; (2)

Int * const a = & B; (3)

Const int * const a = & B; (4)

(1) (2) the meaning is the same, indicating that the memory space indicated by the pointer cannot be modified. * a = XXX; it is invalid.

(3) indicates that the pointer cannot be modified, and a ++ is invalid;

(4) indicates that the pointer and pointer content cannot be modified.

3. const's Function Modification

Void fun (const int & r) (1) is the same as int const

Void fun () const (2)

Const int fun () (3)

(1) The function parameter is const, indicating that the function cannot modify the parameter value.

(2) Add the const suffix to the function. At this time, the function is a member function of the user-defined structure (struct, class), indicating that the function Member cannot modify the member variable of the class.

(3) The return result of the function is const.

Indicates that the returned result cannot be modified. The returned result can only be assigned to the variable modified by const.

 

 

Experience:

1) using a unified encoding style for const variables can reduce unnecessary errors.

2) When passing parameters to a function, try to use the reference method with const as much as possible.

3) The returned results must be modified using 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.