Const full resolution in C + +--go to

Source: Internet
Author: User
Tags modifier

Const-Modified variables are generally written in two ways:
const TYPE value;
TYPE const value;

The two formulations are essentially the same. The meaning of this is that the const-modified variant of type ' value ' is immutable.
For a non-pointer type, no matter how it is written, it is a meaning that value is immutable.

For example:
const int Nvalue;//nvalue is const
int const nvalue;//Nvalue is const

However, there are different ways to style a pointer type, for example:
A. const char *pcontent; (const skips type, modifies *pcontent, so the value cannot be changed)
B. char * const pcontent; (const directly modifies the pointer so that the value of the pointer cannot be changed)
C. Char const *pcontent; (const directly modifies the *pcontent so that the value cannot be changed)
D. const char* Const pcontent; Reading Note 1: is to see * in front of the const or behind, if * in front that is not included in, equivalent to the pointer address can not change, if * in the back that is included in, The equivalent of the value pointed to by the pointer does not change (adornment *pcontent whole, what is *pcontent? is a specific value AH). Reading NOTE 2: Just look at the whole right of the const. On the right is data, that is const data, the right is pointer, and that is the const pointer. The strangest thing is this, plus the const can speed up the parsing speed (because it's possible that the qfileinfo has a cache data structure to handle, and once the const, it doesn't have to be processed, speed up): Http://www.qtcentre.org/threads/61392-QFileInfo-is-very-slow
For the first three types of writing, we can add parentheses to it in a different way.
A. Const (char) *pcontent;
B. (char*) const pcontent;
C. (char) const *pcontent;
This is a glance. According to the rules for const modifiers of non-pointer variables, it is obvious that a=c.
-For A,c, the const-modified variable *pcontent of type char is constant, so the content of pcontent is constant.
A const-modified variable of type char* is pcontent as a constant, so the pcontent pointer itself is constant immutable.
-For D, it is actually a and b mixture, indicating that both the pointer itself and the pointer content are constant immutable

Summarize:
(1) The pointer itself is constant immutable
(char*) const pcontent;

(2) The pointer points to the content is constant immutable, not through the pointer to change the contents of the pointed variable (but through the variable itself to change the content is possible), the pointer itself is a variable
Const (char) *pcontent;
(char) const *PCONTENT;CONST (char*) pcontent;const (char) * pcontent;


(3) Both are not variable
const char* Const pcontent;
There are different ways of doing this:
Line along the * number,
If the const is on the left side of the *, then the const is used to decorate the variable that the pointer points to, that is, the constant is what the pointer points to;
If the const is on the right side of *, const is the modifier pointer itself, that is, the pointer itself is a constant.

2. const modifier function parameters

The Const modifier parameter is one of its broadest uses, which indicates that the value of the parameter cannot be modified in the body of the function (including the value of the parameter itself or the value contained in the parameter). It can be very good
void function (const int Var); The passed arguments cannot be changed within the function (meaningless because Var itself is a formal parameter).
void function (const char* Var); The contents of the parameter pointer are constant
void function (char* const VAR); The parameter pointer itself is a constant invariant (also meaningless, since char* var is also a formal parameter)

Parameters are references, in order to increase efficiency and to prevent modification.
When modifying reference parameters:
void function (const class& VAR);//The reference parameter cannot be changed within the function
void function (const type& Var); The reference parameter is constant within the function is not variable

3. const modifier function return value
The const modifier function returns a value that is not practical, and its meaning is basically the same as the const-modified ordinary variable and the pointer.

(1) const int FUN1 () This is actually meaningless, because the parameter return itself is the assignment.

(2) const int * FUN2 ()
Called when const int *pvalue = FUN2 ();
we can think of fun2 () as a variable, so that's what we said above 1. (1) The notation, that is, the pointer content is immutable.

(3) int* const FUN3 ()
Called when int * Const PVALUE = fun2 ();
We can think of fun2 () as a variable, and that's what we call 1. (2) The notation, that is, the pointer itself is immutable.

4. Const modifier Class object/object pointer/object reference
The const-Decorated class object indicates that the object is a constant object, and any of its members cannot be modified. The same is true for object pointers and object references.
Const-Decorated object, any non-const member function of the object cannot be called because any non-const member function has an attempt to modify the member variable.

For example:
Class AAA
{
void Func1 ();
void Func2 () const;
}

const AAA aobj;
aobj.func1 (); x//Const object cannot call a normal function, you must call the Const function
AOBJ.FUNC2 (); That's right
Const aaa* aobj = new AAA ();
Aobj->func1 (); X
AOBJ->FUNC2 (); That's right

5. const modifier member Variable
The Const modifier class's member function, which represents a member constant, cannot be modified, and it can only be assigned to a value in the initialization list.

Class A
{
...
const int Nvalue; Member constants cannot be modified
...
A (int x): Nvalue (x) {}; You can only assign values in the initialization list

}

6. Const modifier member function
Const modifies a member function of a class, the member function cannot modify any non-const member function in the class. Usually written at the end of the function to modify.

Class A
{
the//function is followed by a const representation that the function cannot modify the members of class. In other words, this function cannot change any data of this instance
void function () const; The constant member function, which does not change the member variable of an object. You cannot call any of the non-const member functions in the class.
}
For const class objects/pointers/references, only const member functions of the class can be called, so the most important role of the Const modifier member function is to restrict the use of Const objects.

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

(1) different compiler processing methods
Define macros are expanded during the preprocessing phase.
Const constants are used during the compile run phase.

(2) Different types and security checks
Define macros have no type, do not do any type checking, just expand.
Const constants have specific types that perform type checking during the compilation phase.

(3) different storage methods
The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.
Const constants are allocated in memory (either in the heap or in the stack

Const full resolution in C + +--go to

Related Article

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.