const keyword usage in C + +

Source: Internet
Author: User
Tags modifier

Why use const? Code written with symbolic constants is easier to maintain, and pointers are often moved along the edge of the read side, rather than the edge-write edges, and many function parameters are read-only and not written. The most common use of const is as an array of bounds and switch sub-condition designator (also can be substituted with enumerators), categorized as follows: Constant variable:ConstA type descriptor variable name is often referenced:ConstType descriptor & Reference Name constant object: class nameConstObject Name constant member function: Class Name:: Fun (formal parameter)ConstConstant group: type specifierConstArray name [size] constant pointer:ConstType specifier * pointer name, type specifier * Const pointer name first prompt is: in the constant variable (const type specifier variable name), constant reference (const type specifier & reference name), constant object (class name const object name), const group (type specifier of the array name [ Size]), the const "and" type specifier "or" Class name "(In fact, the class name is a custom type specifier) where the location can be interchanged. such as: const int a=5; const a=5 with int; The same name and const object name are equal to the Const class name object name. usage 1: ConstantsInstead of the macro definition in C, the declaration must be initialized (not in the!c++ Class). Const restricts how constants are used, and does not describe how constants should be allocated. If the compiler knows all the uses of a const, it can even allocate no space for that const. The simplest common scenario is that the value of a constant is known at compile time and does not require allocation of storage.    -the variable declared with const by the C + + program Language increases the allocation space but can guarantee type safety. In the C standard, const-defined constants are global, and C + + is dependent on the declaration location. Usage 2: Pointers and ConstantsThere are two objects involved when using pointers: the pointer itself and the object it refers to. The declaration of a pointer with a const "pre-pinning" will make that object rather than make the pointer a constant.    To declare the pointer itself as a constant, rather than the object being referred to, you must use the declaration operator *const. So the const that appears before the * is as part of the underlying type:
Char *const//const pointer to char charconst// to Const Char pointer to const char// to const char pointer (the latter two declarations are equivalent)
Right-to-left read memory mode: CP is a const pointer to char. So the PC cannot point to other strings, but can modify the contents of the string it points to PC2 is a pointer to const char. So the contents of *PC2 can not be changed, but PC2 can Point to another string and note that the address of a non-const object is allowed to be assigned to a pointer to a const object, and the address of a const object is not allowed to be assigned a pointer to a normal, non-const object. usage 3:const modifier function passed in parameterDeclare the function passed in as const to indicate that this parameter is used only for efficiency reasons, rather than to allow the calling function to modify the value of the object.    Similarly, the pointer parameter is declared as const, and the function will not modify the object that is referred to by this parameter. Usually modifies pointer parameters and reference parameters:
void Const A *in// modifier pointer-type incoming parameter void Fun (const A& in//  Modifying a reference-type incoming parameter

usage 4: modifier function return valueYou can prevent users from modifying the return value. The return value is also to be paid to a constant or constant pointer accordingly. usage 5:const modifier member function (c + + attribute)Const objects can access only const member functions, not const objects, which have access to arbitrary member functions, including const member functions, members of Const objects cannot be modified, and objects maintained by pointers can be modified, and const member functions can not modify the object's data. Regardless of whether the object is of a const nature. The compilation is checked against the modification of member data.

const keyword usage in C + +

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.