A summary of const usage in C + +

Source: Internet
Author: User
Tags modifier volatile

1. When modifying constants:

const int TEMP1; Temp1 is constant, not variable

int const TEMP2; Temp2 is constant, not variable

2. When modifying pointers:

The main point of view is the const before and after the *, before the pointer to the content of the constant, in the back of the pointer itself is a constant;

const int *PTR; *ptr is a constant;

int const *PTR; *ptr is a constant;

int* const PTR; PTR is constant;

const int * const PTR; *ptr and PTR are constants;

3. When const modifies a class object:

When const modifies a class object, no member of its object can be modified. A const-decorated object that cannot be called by any non-const member function of the object, because any non-const member function will have the possibility of modifying the member variable.

Class temp{

void Func1 ();

void Func2 () const;

}

Const TEMP TEMP;

Temp.func1 (); Error

TEMP.FUNC2 (); Correct

4. Const modifier member Variable:

Const-Modified member variables cannot be modified and can only be initialized in the initialization list, because constants can only be initialized and cannot be assigned;

The assignment is to overwrite the old value with the new value constructor is to open space for it and then assign a value to it, not initialize, while the initialization of the list space and initialization is done at the same time, directly to a value, so the const member variable must be completed in the initialization list.

Class temp{

const int Val;

TEMP (int x) val (x) {}; You can only assign values in the initialization list;

}

5. member functions of the Const modifier class

The const member function indicates that the member function cannot modify any non-const member variable in the class object. The general const is written at the back of the function, such as: void Func () const;

If a member function does not modify a member variable, it is best to declare it as const, because the const member function does not modify the data and, if modified, the compiler will error;

Class temp{

void func () const; Constant member function, cannot modify the member variable in the object, nor can call any non-const member function in the class;

}

For const class objects, only const member functions in the class can be called, so the Const modifier member function is primarily about restricting the use of Const objects.

6. The use of const in function declarations:

In a function declaration, a const can modify the return value of a function, or it can modify a specific parameter;

When modifying a formal parameter, the const constant is initialized with the corresponding variable, and is usually quantified in the function body, according to the part that the const modifies;

When the modifier function returns a value, the const-decorated return value is typically used for overloads of the operator. It is generally not recommended to use the Const modifier's return value type as a case of an object or an object reference;

7. The difference between a const constant and a define macro definition:

1) Different processing stages:

Define is in the preprocessing phase, define constants are never seen by the compiler because they have been replaced in preprocessing truncation;

Const constants are used in the compile phase.

2) different types and security checks

Define no type, no checks, just character substitution, no type safety checks, and unexpected errors in character substitution

Const constants have explicit types, and type checking occurs during the compilation phase;

3) Different storage methods

Define is a character substitution, how many places are used, how many times it is replaced, and no memory is allocated;

The compiler typically does not allocate space for const constants, but saves them in the symbol table, making them a constant during compilation, with no memory read, and high efficiency;

8, mutable key words:

In C + +, mutable is set to break through the limitations of Const. Variables modified by mutable will always be in a mutable state, even if in a const function, even a struct variable or class object is const, its mutable members can be modified:

Class ST {
Public
int A;
mutable int showcount;
void Show () const;
};

void St::show () const{
a=1;//error, cannot modify normal variable in const member function
showcount++;//correct
}

Mutable can only modify non-static data members;

9, Const_cast:

The const or volatile property used to modify the type.

Usage:

:const_cast<type_id> (expression) This operator is used to modify the const or volatile properties of a type.  In addition to const or volatile adornments, the type_id and expression types are the same. 1) The constant pointer is converted into a very good pointer, and still points to the original object, 2) The constant reference is converted to a very good reference, and still points to the original object; 3) Const_cast is generally used to modify the bottom pointer. such as the const char *p form. Recommendation: 1, you should use const whenever possible, it will allow you to specify a semantic constraint (that is, to specify an object that cannot be changed), and the compiler enforces this constraint.  It allows you to tell the compiler and other programmers that a value should remain the same. 2. Declaring something as const can help the compiler detect incorrect usage.  Const can be applied to objects in any scope, function arguments, function return types, member function ontologies, 3, compiler enforces bitwise constness, but you should use "Conceptual constants" (conceptual constness) when you write programs; 4, when the const and NON_CONST member functions have a substantial equivalent implementation, make the NON-CONST version call the const version to avoid code duplication;

A summary of const 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.