This article is reproduced to CSummary of const usage in + +-moonlight woodland-Blog Park http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html1. const modifies ordinary variables and pointer const-modified variables, usually in two ways:ConstTYPE value; TYPEConstThe 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 intNvalue;//Nvalue is a constint ConstNvalue;//Nvalue is a constHowever, there are different ways to style a pointer type, for example: A.Const Char*pcontent; B.Char*Constpcontent; C.Char Const*pcontent;d.Const Char*Constpcontent; For the first three kinds of writing, we can add a new way to it, with parentheses a.Const(Char) *pcontent; B. (Char*)Constpcontent; C. (Char)Const*pcontent; This is a glance. Based on the rules for const-modified non-pointer variables, it is clear that a=C.-For A,c, the const modifier is a variable of type char *Pcontent is constant, so the content of Pcontent is constant.-For B, there is a way to do it:Const(Char*) pcontent; meaning: const-Modified type is char*the variable pcontent is a constant, so the pcontent pointer itself is a constant variable.-for D, it is actually a and b mixture, indicating that both the pointer itself and the pointer content are constant invariant summaries: (1The pointer itself is constant immutable (Char*)Constpcontent;Const(Char*) pcontent; (2the content pointed to by the pointer is constant immutableConst(Char) *pcontent; (Char)Const*pcontent; (3) both are immutableConst Char*ConstPcontent; There's a difference: along the way*line if the const is located in the*To the left, the const is used to modify the variable that the pointer points to, that is, the pointer is a constant, and if the const is in*on the right, const is the modifier pointer itself, that is, the pointer itself is a constant. 2The . Const modifier parameter of 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 goodvoidfunctionConst intVar);//the passed arguments cannot be changed within the function (meaningless because Var itself is a formal parameter) .voidfunctionConst Char* Var);//the contents of the parameter pointer are constant immutablevoidfunctionChar*ConstVar);//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:voidfunctionConstclass& Var);//reference parameters cannot be changed within a functionvoidfunctionConsttype& Var);//reference parameters are constants within a function immutable 3.ConstThe return value of the Modifier function returns the value of the const modifier function is not much, its meaning is basically the same as the Const modifier common variable and the meaning of the pointer. (1)Const intfun1 () This is actually meaningless, because the parameter return itself is the assignment. (2)Const int*when Fun2 () is calledConst int*pvalue =fun2 (); we can think of fun2 () as a variable, and that's what we call 1. (1), that is, the pointer content is immutable. (3)int*Constwhen Fun3 () is calledint*ConstPValue =fun2 (); we can think of fun2 () as a variable, and that's what we call 1. (2), that is, the pointer itself is immutable. 4. Const modifier Class object/object pointer/an object reference to a const-decorated class object indicates that the object is a constant object, and any member of it 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:classaaa{voidfunc1 ();voidFunc2 ()Const;}ConstAAA aobj;aobj.func1 (); Xaobj.func2 (); correctConstaaa* aobj =NewAAA (); Aobj-func1 (); Xaobj-Func2 (); correct5the. Const modifier member variable is a member function of the const-decorated class, which represents a member constant, cannot be modified, and it can only be assigned to a value in the initialization list. classa{ ...Const intNvalue;//member constants cannot be modified..... A (intx): Nvalue (x) {};//you can only assign values in the initialization list} 6The . Const modifier member function of the const-decorated class is a member function that cannot modify any non-const member function in the class. Usually written at the end of the function to modify. classa{ ...voidfunction ()Const;//constant member function, which does not change the member variable of an object. You cannot call any non-const member functions in the class. for Const class objectsPointerreference, only the const member function of the class can be called, so the most important role of the Const modifier member function is to restrict the use of the const object. 7the. Const constant differs from the Define macro definition (1compiler handles different define macros are expanded during the preprocessing phase. Const constants are used during the compile run phase. (2types and security checks differ 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. (3stored in different define macros are only expanded, how many places are used, how many times they are expanded, and no memory is allocated. Const constants are allocated in memory (either in the heap or in the stack).
Summary of const usage in C + +