Mutable and const in C ++

xiaoxiao2021-03-06  44

The CONST key in C is used to represent a constant, and the const is also used to modify the function. I am in this clear concept is: Const modified function can only be a member function of the class, because in the modified function modified, the compiler is not modified by the compiler. Relative, Mutable is a member variable used to modify the class, so that the variable can be modified in the member function modified by Const. And the const modified function can only be a member function of the class, and the Mutable modified variable can only be a member variable of the class. It's just a pair of family directions ~

There have been three problems here:

First: Why is the member variable of the protection class is not modified

Second: Why protect the member variable with const, but also define a mutable keyword to break through the CONST blockade line?

Third: Is there any two keywords that do not use const and mutable?

My opinion on these three questions is that the member variables of the protection class are not modified in the member function. To ensure the logic of the model is correct, by avoiding the state of modifying the class object in the function by using a const keyword. And in all places where the member function can be used more accurately, the impact of the blending of the member function can be predicted. Mutable is to break through the CONST blockade line, allowing some of the secondary or adjuvant member variables that can be changed at any time. No use and mutable keywords are of course not fault, the const and mutable keywords are only given more design constraints and design flexibility to modeling tools, and programmers can handle more logical inspection issues to compilers and build. The mold tool is done, thereby alleviating the burden of programmers (I think this is just to handle the burden to the designer ~, and did not reduce any workload). If the development process has a stricter iterative process, using these two keywords should be more effectively reflecting their role.

转载请注明原文地址:https://www.9cbs.com/read-76125.html

New Post(0)