AboutC/C ++MediumConstWe have introduced a lot before. You can take a look at these two articles, "C ++ basic details on const usage" and "C ++ beginner's const Usage Details". for reference.
Today, when I learned section 4.1 of Essential C ++, I found that when const modifies a member function of a class, it does not only prohibit the modification of data in the class, but also requires that all the members of the class used in it have the const attribute. For example:
- // CONST.CPP
- #include <iostream>
- using namespace std;
- class MyClass
- {
- public:
- MyClass();
- int getValueA();
- int getValueB()const;
- private:
- int a;
- int b;
- };
- MyClass::MyClass()
- {
- a = 1;
- b = 2;
- }
- inline int MyClass::getValueA()
- {
- return a;
- }
- inline int MyClass::getValueB()const
- {
- cout <<"value a: " <<getValueA();
- return b;
- }
- int main()
- {
- MyClass myclass;
- cout <<endl;
- cout <<"value a: " <<myclass.getValueB();
- return 0;
- }
In the above Code, the non-const member function getValueA () is called in the getValueB () member function modified by const in MyClass. This code will prompt an error during compilation:
IDE: V. S. 2008
- Const. cpp
- D:/Program Files/Microsoft Visual Studio 9.0/VC/include/xlocale (342): warning C
- 4530: The C ++ exception handler is used, but the extended semantics is not enabled. Please specify/ESCs
- Const. cpp (29): error C2662: "MyClass: getValueA": the "this" pointer cannot be taken from "const"
- Convert MyClass to MyClass &"
- Conversion loss qualifier
IDE: CODEBLOCKS
- F:/Programs/CodeBlocks/C++/Const/const.cpp||In member function 'int MyClass::getValueB() const':|
- F:/Programs/CodeBlocks/C++/Const/const.cpp|29|error:
- passing 'const MyClass' as 'this' argument of 'int MyClass::getValueA()' discards qualifiers|
- ||=== Build finished: 1 errors, 0 warnings ===|
You can correct the preceding error after adding the const modifier to getValueA.
Appendix:
These may have been pointed out in some books, but I haven't met them yet. Now that I have learned it, I will write it down with a memo. In addition, we will summarize other points of const below.
1. When const modifies constants and references
"Read-only" attribute.
2. When the qualifier const and pointer
There are two usage methods: one is the pointer to the const object, and the other is the const pointer.
3. const can improve compilation Efficiency
The compiler usually does not allocate memory space for const constants during compilation, but stores it in the symbol table, which is clearly explained in "Programmer self-cultivation, when it is null ). Less storage and read memory operations.
Note:
1. When using the const object pointer A, although the pointer cannot be used to modify the object to which it points, but it does not mean that every object pointed to by this pointer will be well protected, because this object itself may have been "stolen ". The reason is that "the address of a non-const object is a pointer that can be assigned to the const object ".
2. the const qualifier can be placed before or after the type, which is prone to errors when the const type definition is written using typedef. Therefore, it is better to put the const behind the type.
Now let's think about it as a matter of course, because only when the member function called in the const member function does not modify the object can we ensure that the const member function does not change the object. Therefore, it is safe to declare the called member function as const.