C ++ - const keyword

 

const variables, once assigned, can not be modified

If the source code, const variable is modified, the compiler error 'l-value specifies const object'

(A) a constant pointer vs pointer constant face questions [2-7]

 1  #include <stdio.h>
 2  
 3  int main()
 4  {
 5      const int x = 1;
 6      int b = 10;
 7      int c = 20;
 8  
 9      const int* a1 = &b;  
10      int* const a2 = &b;
11      const int* const a3 = &b;
12  
13      x = 2;     ! // wrong Line 5 const int x = 1 can not be modified assignments
 14   
15       A1 = & C;    .. 9 // Right Line is modified in const * a1, * a1 type int, a1 itself may modify
 16       * = A1 . 1 ;    // * A1 is Wrong const, can not be modified, an error value specifies const Object-L!
 . 17   
18 is       A2 = & C;    // const Wrong Line 10 in modification a2, a2 is const, can not be changed!
 . 19       * = A2 . 1 ;   // right * a2 is int type, can be modified.
 20   
21       a3 = & c;    // Wrong Line 11 in * a3 and a3 are const, can not be changed!. 
22       * a3 = 1 ;    // Wrong Line 11 in * a3 and a3 are const, can not be changed!
23  
24      return 0;
25  }

(Ii) Description of characteristics and differences #define const face questions of [2-8]

(2.1) #define

1 #define PI 3.1415926 2 float angel; 3 angel = 30 * PI / 180;

#define: during pre-compilation procedure, of all the PI replaced '3.1415926', then compiled. 

#define constant is a "Compile-Time" concept, so it has a code segment and procedures , it did not really sense in the actual operation

(2.2) const

const variable exists in the data segment of the program and take forward the allocated space.

const is a constant 'Run-time' concept. It really and actually exist and can be invoked, passing in the program.

const data type, # define no data types.

The compiler can perform type const constant security checks.

(C) C ++ const in what role? [2-9] face questions

  (1) const for defining constants; const constants defined, the compiler may be a static type safety data

    (2) const parameters modified functional form: When the type of custom data types and abstract, it should be 'pass values' as a user input parameter to 'const & transmitted address', efficiency can be improved.

1 void fun(A a);
2 void fun(A const &a);

    The above code, the efficiency is higher than Line 2 Line 1, because the 'pass by reference' need to generate temporary variables, there is no structure, replication, the destructor. However, A & a reference light may change a, so labeled const

  (. 3) const Returns the value of the modified function returns the value can not be modified, while requiring only be assigned to return value const variable modification of the same type

. 1 #include <stdio.h>
 2  const  int Fun () { return  . 1 ;}
 . 3  int main () {
 . 4      const  int m = Fun (); // const receiving the return value of the function with variable returns const
 . 5      return  0 ;
 6 }

  (4) member function const modified class: does not modify any data application const member functions are modified, so that when accidentally modify the data members or call a non-const member functions, the compiler will report an error. Const member functions in the form of a modified class

int GetCount(...) const;

[Note] The above expression const int GetCount (...) different, const int GetCount (...) represents the return value is an int constant

【example】

 1 class Student{
 2     public:
 3       Student(char* name, int age, float score);
 4       void show();
 5       char* getname() const;
 6       int getage() const;
 7       float getscore() const;
 8    private:
 9       char *m_name;
10       int m_age; 
11       float m_score;
12 };
13 
14 Student::Student(char* name, int age, float score):m_name(name),m_age(age),m_score(score){}
15 void Student::show(){
16    cout<<m_name<<"'s age is "<<m_age<<" with score of "<<m_score<<endl;
17 }
18 char* Student::getname() const {return m_name;}
19 int Student::getage() const {return m_age;}
20 float Student::getscore() const {return m_score;}

Define and implement const member functions of the class [Note] are required at the same time at the end of the function header plus the const keyword. char * getname () const to char * getname () function prototypes are two different, if only in one place, plus const causes the function prototypes and definitions of life conflicts at.

  The only way to (5) const member variable initialization is to use the initialization list

. 1  class VLA { // correct version
 2      Private : 
 . 3         const  int m_len;
 . 4         int * m_arr;
 . 5      public :
 . 6         VLA ( int len);
 . 7  };
 . 8  // must be initialized using the initialization list m_len 
. 9 VLA VLA :: ( int len): m_len (len) {
 10      m_array = new new  int [len];
 . 11 }
class VLA { // wrong version
    Private :
        const  int m_len;
        int * m_arr;
    public :
       VLA ( int len);
};

:: VLA VLA ( int len) {
   m_len = len;   // const int m_len not here be initialized 
   m_arr = new new  int [len];
}

 

Reference (Reference)

[1] C and C ++ programmers interview tips - Selection of the most common C / C ++ interview Zhenti: Dongshan Hai 

[2] http://c.biancheng.net/view/2230.html

[3] http://c.biancheng.net/view/2223.html

Guess you like

Origin www.cnblogs.com/jg01/p/12400227.html