C ++ const

Source: Internet
Author: User

Record important c ++ learning records


1 const understanding:
1. const modifies Common variables and pointers
The const modifier variable can be written in two ways:
Const TYPE value;
TYPE const value;


These two methods are essentially the same. It means that the value of the TYPE variable modified by const is unchangeable. For a non-pointer TYPE, no matter how you write it, it is a meaning, that is, the value is only unchangeable.


For example:


Const int nValue; // nValue is const
Int const nValue; // nValue is const


However, different types of pointer types may be written in different ways, for example:

A. const char * pContent;
B. char * const pContent;
C. char const * pContent;
D. const char * const pContent;
 


For the first three writing methods, we can add brackets to them.


A. const (char) * pContent;


B. (char *) const pContent;


C. (char) const * pContent;


In this way, you can see at a glance. According to the rules for modifying non-pointer variables in const, it is obvious that A = C.


 


-For A, C, and const variable whose type is char * pContent is A constant, the content of pContent is constant and immutable.


-For B, there is actually another way of writing: const (char *) pContent;


Meaning: The variable pContent whose const type is char * is constant. Therefore, the pContent pointer itself is constant and immutable.


-For D, it is actually A mixture of A and B, indicating that both the pointer itself and the pointer content are constants immutable.


 


Summary:


(1) the pointer itself is a constant that is unchangeable.

Www.2cto.com
(Char *) const pContent;


Const (char *) pContent;
Can be simply understood as const in *
 


(2) The Pointer Points to a constant that is unchangeable.


Const (char) * pContent;


(Char) const * pContent;


 


(3) Both are unchangeable.


Const char * const pContent;


 


There are also different methods:


Draw a line along the number,


If the const is on the left side of *, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;


If const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.


2. const modifier function parameters


Const modifier is the most widely used function parameter. It indicates that the parameter value cannot be modified in the function body (including the value of the parameter itself or the value contained in the parameter ). It can be good
Void function (const int Var); // The passed parameters cannot be changed in the function (meaningless, because Var itself is a form parameter)
Void function (const char * Var); // The content indicated by the parameter pointer is constant immutable

 


Void function (char * const Var); // The parameter pointer itself is a constant and immutable (also meaningless, because char * Var is also a form parameter)


 


The parameter is a reference. To increase efficiency and prevent modification.
When modifying referenced parameters:
Void function (const Class & Var); // The referenced parameter cannot be changed in the function.
Void function (const TYPE & Var); // The referenced parameter is a constant in the function and cannot be changed.

 


3. const modifier function return value
The const modifier function does not actually return a lot of values. Its meaning is basically the same as that of the const modifier for common variables and pointers.
(1) const int fun1 () is meaningless because the return value of the parameter itself is a value.


(2) const int * fun2 ()


Const int * pValue = fun2 ();


We can regard fun2 () as a variable, which is the method of 1. (1) mentioned above, that is, the pointer content is immutable.


(3) int * const fun3 ()


Int * const pValue = fun2 ();


We can regard fun2 () as a variable, so it is the method of 1. (2) mentioned above, that is, the pointer itself is immutable.


4. const modifier Class Object/Object Pointer/Object Reference


The const modifier Class Object indicates that the object is a constant object, and no member can be modified. The same is true for object pointers and object references.


Const modified object, any non-const member function of this object cannot be called, because any non-const member function will attempt to modify the member variable.


For example:


Class AAA
{
Void func1 ();
Void func2 () const;
}


Const AAA aObj;


AObj. func1 (); ×


AObj. func2 (); correct


 


Const AAA * aObj = new AAA ();


AObj-> func1 (); ×


AObj-> func2 (); correct


 


5. const modifies member variables


A member function of the const modifier class, which indicates a member constant and cannot be modified. It can only be assigned a value in the initialization list.


 


Class


{


...


Const int nValue; // The member constant cannot be modified.


...


A (int x): nValue (x) {}; // values can only be assigned to the initialization list.


}


 


6. const modifies member functions


If a const modifies a member function of a class, the member function cannot modify any non-const member function of the class. It is generally written at the end of the function.


 


Class


{


...


Void function () const; // a regular member function. It does not change the object's member variables, nor can it call any non-const member functions in the class.


}


For const class objects, pointers, and references, only the const member functions of the class can be called. Therefore, the most important function of const member functions is to restrict the use of const objects.


 


7. Differences between const constants and define macros


(1) Different compiler Processing Methods


The define macro is expanded in the preprocessing phase.


Const constants are used in the compilation and running phase.


(2) Different Types and security checks


The define macro has no type and does not check any type. It just expands.


Const constants have specific types. during compilation, the type check is performed.


(3) Different Storage Methods


The define macro is just an extension. If it is used in many places, it is expanded as many times without allocating memory.


Const constants are allocated in memory (either in heap or stack ).


From the column zhaoguoxian12345

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.