Effective C + + Clause III use const whenever possible

Source: Internet
Author: User

Reference: Summary of common usages of http://blog.csdn.net/bizhu12/article/details/6672723 const

1. Used to define a constant variable so that the variable can no longer be modified at a later

const int val = 90;

val = 100; Error

2. The parameter is not modified when the parameters are protected, if the value of this parameter is modified by using a reference to pass the parameter or pass the argument by address,

The value of the variable passed in by the function is also changed, and if you want to protect the passed variable from being modified, you can use the Const protection

void fun1 (constint &val)
  {
      val = 10; Error
  }
  void fun2 (int &val)
  {
      That's right
  }

3. Save memory space

#define PI 3.14
Const Double
Double  A = PI;  //The pi is allocated memory at this time, but there is no memory allocated after this definition.
Double  b = PI;  //compile-time memory allocation
Double  c = pi;  //No more memory is allocated,
Double  d = PI;  //compile-time redistribution of memory
A const-defined variable that allocates memory only once, and macro constants, defined by # define, can be allocated many times, so that the const is space-saving

4. Use the const modifier function in a class to prevent modification of non-static class member variables

  class A
  {
  Public:
      void Const
      {
          A = 10; Error, non-static variable cannot be modified
          b = Ten;   //Correct, you can modify the static variable
      }
  protected:
  Private:
      int A;
      Static int b;
  };

This is primarily used to determine which function can be used to alter the object's content, and which functions cannot be used to change the object's contents.

5. Modifier pointers

const int *a; int const *A; The same effect, mainly the position of the * and the const, on the left side, represents the modifier, on the right side represents the modifier pointer.

    int m = ten, j = 100;
    Const int *p1 = &m; //const on the left, indicating the modifier, *p immutable. P variable. 
    printf ("%p,%d\n", P1,*P1);
    *P1 = 100;//error, *P1 immutable, but does not specify P1 immutable, p1 can point to other variables.
    P1 = &j;
    printf ("%p,%d\n", P1,*P1);

In the above program, we can also change the value of M, which is possible.

6. modifier function return value to prevent the return value from being changed

const int fun ();

7. Modifying member variables of a class

In this case, initialization is a place to be aware of.

If it is a const-modified variable, it can not be initialized directly inside the class, but must be initialized in the constructor initialization list. Static modified variables must be initialized outside of the class. When it is a const and static modified variable, it must be initialized outside of the class.

  class A
  {
  Public:
     const int val = 100; Error
     static int val1 = 100; Error
      Const int val;
      Static int val1;
      Const Static int val2;
      The const variable must be initialized in the constructor initialization list
      {
      }
      void Const
      {
          A = 10; Error, non-static variable cannot be modified

b = Ten; //Correct, you can modify the static variable

val = 100; Val is a const type and cannot be modified

      }
  protected:
  Private:
      int A1;
      Static int b;
  };
  int a::b = 100;
  int a::val1 = 100; //static variables must be initialized outside of the class
  Const int a::val2 = 100;//const Static retains the static property and must be initialized outside the class

8.const defines an object variable that can only be used by this program in a C + + file, and cannot be called by other C + + files of the program

A const-defined object variable can be called by another file, and must be defined using the extern modifier as

extern const int Val;

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.