C + + Const Qualifier Summary

Source: Internet
Author: User

1, the Const object once created its value can no longer change, so be sure to initialize.
2. The const type can only perform operations that do not change its contents, such as participating in calculations, assigning values, etc.
3, if you want to share a const in more than one file, you can add extern at the definition or declaration of a const, although it is defined directly in the header file, it can be seen in other files, but if it is declared in the header file only, it is not shared in the CPP implementation. eg.//Test.h#ifndef __test__test__#define __TEST__TEST__ #include<stdio.h> externconstint value; #endif/* Defined (__test__test__) */
//Test.cpp #include"test.h" int func () {
return ;} externconstint value =func();
//Main.cpp #include<iostream> #include"test.h" externconstint value; int Main ( int argc, const char * argv[]) { std: :cout <<value << std:: Endl; return 0; }
4, the constant reference does not exist, because the reference itself cannot change the reference object, in this sense, all references are considered constant. The constant reference that we generally refer to is actually a reference to the const, asconst int CI = 1024x768;const int &CRI = CI;
5, when initializing a constant reference allows an arbitrary expression as the initial value, as long as the result of the expression can be converted to a reference type, that is, the following statement is legal:const int &R1 =;const INT &R2 = r1 * 2;
6. A reference to Const allows referencing an object that is not const, but cannot modify the original value by the resulting reference:int i = 1024x768;const int &CRI = i;i = 512;//correctCRI = 512;//Error
7, the pointer constant is a pointer to a constant, the constant pointer is that the pointer itself is a constant. Constant pointers must be initialized. The way to distinguish between the two is to see * on the left or right of the const, the left value is a constant, the right pointer is a constant.
8. Const object cannot call normal member function The type of this in a class by default is a constant pointer to a very large version of the class type, = = a bit around, for example: MyClass * Const type. Although this is implicit, we cannot bind it to a constant object, which leads to the inability to invoke normal member functions on a constant object. If we try to turn this into a const MYCLASS * const can solve the problem, C + + allows us to add the Const keyword to the parameter list of the member function, that is, the shape as follows, we call the constant member function.string to_string () const {return "haha";}A constant member function cannot change its object content, but it is no problem to invoke the internal data member. A constant object, as well as a reference to a constant object, or a pointer can only call a constant member function. But a constant member function can be called by ordinary variables, such as the C_str () method in string.
9. The const function can call a static member function in addition to the constant member function (perhaps because the static function does not modify the static member variable).

C + + Const Qualifier Summary

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.