C + + const

Source: Internet
Author: User
Tags modifier

1.const modifier variables
int Base=0;//Define constants (read-only variables in a class can only and must be initialize in the initialization list)//The const constant simply gives the corresponding memory address, rather than the immediate number given by # define, so the const-defined constant//There is only one copy while the program is running, and the constant defined by # define has several copies in memory. Const intA1 =1;//The compiler can type security checks on it (macro constants are simply replacements, which can produce unexpected errors)int ConstA2 =1;//modifier Pointersint*ConstB1 = &Base;//simultaneous modifier values and pointersint Const*ConstC1 = &Base;Const int*ConstC2 = &Base;//Const modifier Memory: The left side of the keyword, which itself modifies the right side at the far left

2.const modifier function (parameter, return value, function itself)
classcode{CString M_code; Public: Code () {}~Code () {}Constcstring* GetCode (Constcstring& src)Const; voidSetcode (Constcstring&src);};
voidCode::setcode (Constcstring&src) {M_code=src;}Constcstring* Code::getcode (Constcstring& src)Const{ // error //src = result; // Const-Modified variable is read-only and within a read-only function // m_code = src; // Const-Decorated class member functions cannot modify members // Setcode (SRC); // Const-Decorated class member functions can only call read-only member functions return&M_code;}

The return value of the GetCode () function must be in const cstring* RE = Code.getcode (_t ("GB1001")); or write Auto re = Code.getcode (_t ("GB1001"));

This read-only guarantee is a compiler-imposed limitation, and in rare cases, to modify the properties of an object within a read-only function, you can do this:

Const cstring* Code::getcode (const-const{     //  1.mutable keyword modifier mutable int value;     // 2. Bypassing the compiler's limitations    code* p = (code*)this//  key is this strong switch to code*    p->setcode (src)    ;  return &M_code;}
Const and function overloading
void Code::fun (int a)const//  can be overloaded, depending on whether the object is read-only difference {}void Code::fun ( int a) {}void code::fun (constint//  cannot overload {}void code::fun (int  a) {}

3.const decorated objects

Const-decorated objects can only access read-only functions in a class and cannot modify variables. A good design should be a const-qualified method, an unrestricted object, a property in a class can only be passed by means of the Get () or Set ()

4. Read-only iterators

5.const and Smart pointers

6.const differs in C and C + +

Const in C + + is generally considered a compile-time constant and does not allocate space for const, except that it is stored in the symbol table at compile time, and is referred to in the code at the appropriate time.
In C + +, whether to allocate space for const depends on the specific situation, if you add the keyword extern or take the const variable address, the compiler will allocate storage space for the const
The const in C is a normal variable that can not be changed, which takes up storage space

Const int Ten ; int 0 // Error in C: compiler does not know the value at compile time (array subscript must be explicit)

7.const_cast keywords
// const_cast The const or volatile qualifier used to remove a variable ://The consequences of using const_cast re-writing are undefined, and the purpose of const_cast is not to allow you to modify a value that itself is defined as const,//because the consequences of doing so cannot be expected. The purpose of const_cast is to modify some pointers/reference permissions,//If we were unable to modify the value of a block of memory through these pointers/references, now you can. Const intA =1;//int* temp = A;//Errorint* PA1 = const_cast<int*> (&a);//removing the const limitint* PA2 = (int*) &a;//traditional way to remove const-Limit*PA1 =Ten;intModify1 = A;//1, here is directly replaced by a constant expression, no longer value from the address (const constant introduced in the original intention is to replace the # #)*PA2 = -;intModify2 = A;//1

C + + 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.