C ++ is used on const keyword

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/bua200720411091/article/details/39718505
const keyword is used to indicate a variable or function, or that other types of data value or the value itself is prohibited from being modified.

        Note, however, even modified to a variable of type const, which is still essentially a variable rather than constant. 
you might say, what difference does it make? I feel no difference in the code ah. in fact, they differ compiler compiler. for the compiler, variables implies the need to allocate memory space, but only constants a license is recorded in a memory compiler's integer table inside it. the value of the variable must be initialized at compile time, unless it is modified to use extern, which tells the compiler that the variable is a global const variable, where the definition of the compiler temporarily I do not know, so allowing it to define their value in other places, once again revise the definition of good after the ban. 

        more complex situations, const variables modified unexpected circumstances:
1. the X-const char * = "Diamond"; // the X-ban modified, but * (x) can be modified: the X-* = "kongdom"; the OK | the X-++; ERROR !!!
2. const char * S = "Diamond"; // * (S) prohibit modification, it The string should not be changed: * S = "Kingdom"; ERROR !!! | S ++; the OK

3. the Person P_01;
    const the Person * = & P_01 the p-; // the object is const
    the Person * = & P_01 the p-const;           
    Person * const p = & P_01; // const pointer is 
    const Person * const p = & P_01; // const objects and pointers are 


        then in the end is recommended to use const Under what circumstances do?
1. modification function parameters:
                We all know that in the c ++ function parameter passing, there are passed by value, passing a pointer passed by reference, and three forms when both parameters passed after use, the function is called, the value of argument will be operated to modify the internal functions, so if the need to modify it is not passed by reference pointer and it can not be moved to the answer of course is no, this time you only need to modify variables using const in a function parameter list on it such as:?.
                void FUNC_01 (the X-const int *); // Do not modify this compiler will pass into the function, the pointer points to the memory space, the value of
                int a = 100;
                FUNC (& a); // be a push
                const int b = a; // to a places the value of b is                        func (& b); // b push the
                

                ! b = a ++ // ERROR value of b should not be changed

                void func_02 (const int i)
                {
                        i ++;! // ERROR not be changed, the value of i
                }


2. The modified member function
        which means that this is const
                int get_age () const ;                 int Student :: set_age (int Age)                 {                         this.age = Age;                 }                 int Student :: get_age () const                 {                         this.age ++;! Do not modify the member variables // ERROR                         set_age (16);! // ERROR ban call no cosnt modified member
                









                        Age return; // the OK                 }   3. Object classes modified               target value which prohibits the modification   4. Overload of Functions     provided: an inner member function has the following Student class:                 void FUNC ();                 void FUNC () const;     and is: main () function there:                 const Student STU;                 stu.func ();     then: more than two functions constitute function overloading, you can implement static polymorphism.     you might ask, these two functions should be repeated define ah ., should they be wrong not constitute function overloading but in fact not the case..     where two func () function, all belong to the class member functions, but they are actually this:             void FUNC (the this Student * );             void FUNC (const Student * this);      that is, the second FUNC () const modifier function, is used to modify the class object itself, this is
                        


                



















     When const object of type stu, call the func () function will be to call the same type of const is that func () const function.




Guess you like

Origin blog.csdn.net/bua200720411091/article/details/39718505
Recommended