c ++ const usage

1. General modification variable, const int a = 10; indicates that this variable can not be modified, straightforward, not more than that

2. modified pointer, mainly the following three

     const int * & CP1 = a; // modified int, not modify the value of the memory pointer, i.e., a value of

     int const * & CP2 = A; // supra

     * int const CP3 = & a; // * modification, the pointer itself can not be changed, i.e. can not be changed or a pointer to b

    Mnemonic: const look left first, what is the modification of the above type, the left does not look first to the right

  int A = 10 ;
   int B = 20 is ; 
  STD :: COUT << " A = " << << A STD :: endl; 
  STD :: COUT << " B = " << << B STD :: endl; 
   
  // . 1 
  const  int * & CP1 = a ; 
  STD :: COUT << " * CP1 = " << << CP1 * STD :: endl; 
  CP1 = & b;      //         can modify pointer itself, i.e., a point can be changed or b
   // * cp1 = 1111;   // error, modified int,Not modify the value of the memory pointer, i.e., a value of
  std::cout << "after cp1=&b,*cp1 = " << *cp1 << std::endl;

  //2  the same as 1
  int const *cp2 = &a;
   std::cout << "*cp2 = " << *cp2 << std::endl;
  cp2 = &b;
  //*cp2 = 2222;  //error
  std::cout << "*after cp2=&b,*cp2 = " << *cp2 << std::endl;

  //3
  int *const cp3 = &a;
  std::cout << "*cp3 = " << << CP3 * STD :: endl;
   // CP3 = & B;     // error, * modification, the pointer itself can not be changed, i.e. can not be changed to point or a B 
  * CP3 = 3333 ;   //        can modify the pointer memory value, i.e. a value 
  STD :: COUT << " After 3333 = * CP3, CP3 = * " << << CP3 * STD :: endl; 

  STD :: COUT << " a = " << << a :: endl STD; 
  STD :: COUT << " B = " << STD :: B << endl;

3. Modified class member functions

     const int setValue(const int value) const

     From left to right, a return value can not be changed

                       2. The parameters can not be changed

                       3. regular function, a function of a member variable can not be modified, such as the need to change, add mutable member variables before

class A 
{ 
public : 
  A (): M_A ( 0 ), M_B ( 0 ) {}
   Virtual ~ A () {} 
 
public :
   const  int Geta () { return M_A;}   // return value can not be modified 
  int the setValue ( const  int value) const 
  { 
      // M_A = value; // later have const error function, the function often can not modify a member variable 
      M_B = value;    //       if you want to edit, modify mutable member variables need to add
       // value ++;      // error const modifier function parameters can not be modified 
      return  value;
  }

private: int m_a; mutable int m_b; };

 

Personal summary:

     In practice, generally modified by const complex data structures or custom class pointer or reference to a function or return parameter passing, to avoid to copy data or parameter passing to generate a temporary object, affect the performance of the program, often recommend such a deal, try to use const pointers or references to deal with mass participation , try to get the compiler to optimize the maximum extent.

      For the average base-type variable itself when values ​​are passed, little significance.

 

Guess you like

Origin www.cnblogs.com/leehm/p/10972970.html