Discuss the usage of const in C/C ++

Source: Internet
Author: User

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:

 
 
  1. // CONST.CPP  
  2. #include <iostream>  
  3. using namespace std;  
  4. class MyClass  
  5. {  
  6. public:  
  7. MyClass();  
  8. int getValueA();  
  9. int getValueB()const;  
  10. private:  
  11. int a;  
  12. int b;  
  13. };  
  14. MyClass::MyClass()  
  15. {  
  16. a = 1;  
  17. b = 2;  
  18. }  
  19. inline int MyClass::getValueA()  
  20. {  
  21. return a;  
  22. }  
  23. inline int MyClass::getValueB()const 
  24. {  
  25. cout <<"value a: " <<getValueA();  
  26. return b;  
  27. }  
  28. int main()  
  29. {  
  30. MyClass myclass;  
  31. cout <<endl;  
  32. cout <<"value a: " <<myclass.getValueB();  
  33. return 0;  
  34. }  

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

 
 
  1. Const. cpp
  2. D:/Program Files/Microsoft Visual Studio 9.0/VC/include/xlocale (342): warning C
  3. 4530: The C ++ exception handler is used, but the extended semantics is not enabled. Please specify/ESCs
  4. Const. cpp (29): error C2662: "MyClass: getValueA": the "this" pointer cannot be taken from "const"
  5. Convert MyClass to MyClass &"
  6. Conversion loss qualifier

IDE: CODEBLOCKS

 
 
  1. F:/Programs/CodeBlocks/C++/Const/const.cpp||In member function 'int MyClass::getValueB() const':|  
  2. F:/Programs/CodeBlocks/C++/Const/const.cpp|29|error: 
  3. passing 'const MyClass' as 'this' argument of 'int MyClass::getValueA()' discards qualifiers|  
  4. ||=== 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.

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.