4.const effect of (unfinished)

The role of 4.1.const

(1) define constants

(2) ease of type checking

const constant data type modification, but no macro constants, the constants const type safety checks can be carried out, the absence of this step, there is an exception error may occur.

(3) can be modified to protect something

Prevent accidental modification.

(4) can easily be modified parameter

(5) provide reference for overloads

 

. 1  class A {
 2      void FUNC () {};
 . 3      // Overload func (), are not allowed to use a non-member function in the parentheses after the const 
. 4      void FUNC () const {};
 . 5 };

 

(6) to save space

const define constants given memory address, the process of running only one copy, and macro definitions multiple copies in memory.

(7) to improve efficiency

The compiler does not allocate storage for ordinary const variable, but the presence of the symbol table, and the memory read and store operations during compilation, efficiency is improved.

4.2.const use

(1) By default, const objects do effectively in the file, if you want to share in several files, using extern keyword it!

(2) const reference

. 1      int I = . 5 ;
 2      // allowed to bind to the great amount 
. 3      const  int & R_1 = I;
 . 4      // allowed to bind to the literals 
. 5      const  int & R_2 = 10 ;
 . 6      // allowed to bind to type double reference 
. 7      const  Double & R_3 = I;
 . 8      // allow the right of the equal sign with an operational expression 
. 9      const  int & r_4 = I + . 5 ;
 10      const  Double & r_5 = I + . 5 ;

A binding constant reference to what happened on another type?

For example, when the fifth line is bound to a literal constant, the compiler is doing:

1     int temp = 10;
2     const int &r_2 = temp;

 

First of all the literals stored with a temporary object, so that r_2 binding is a temporary object. The reason C ++ primer 5th explained P62-page, why do not const modified if the fifth line of code above will be wrong in this, it is pointed out that the const qualifier obscure role actually show that this indirect references will not be changed object reference , the following is a description:

 

. 1      int I = . 5 ;
 2      // allowed to bind to the great amount 
. 3      const  int & r_1 = I;
 . 4      // error allowed by the binding value of the object & r_1 changing 
. 5      & r_1 ++ ;
 . 6      // properly used objects own operation 
. 7      I ++;

 

(3) and a pointer const

Constant pointer:

 1 double p = 3.14;
 2     double p1 = 3.15;
 3     //允许将一个普通变量取地址赋值给一个指向常量的指针,反之,不允许一个将一个常量取地址赋值给一个普通指针
 4     double const *p_1 = &p;
 5     double const *p_2 = &p1;
 6     //正确,允许改变指针所指向的地址(改变指针本身)。
 7     p_1 = p_2;
 8     //错误,不允许改变指针所指地址存储的值
 9     *p_1 = 50;
10     cout << *p_1;
11     

 

常量指针:

    double pi = 3.14;
    double pi_1 = 3.16;
    //不能够把一个常量取地址赋值给一个指针常量,因为显然等号右侧应该是一个可变的值
    double * const pip = &pi;
    //允许通过指针修改所指向地址存储的值
    *pip = 3.15;
    //错误,不允许修改指针常量中指针本身
    pip = &pi_1;

 

如何阅读一个定义?

       在不带有数组的情况下,先读()的内容,然后从左往右读。比如说,double const *p_1 = &p;  先找到标识符p_1从左往右:*修饰,表明其首先为一个指针,然后是const修饰,说明这个指针所指的对象是一个常量,然后是基本类型符double,于是我们可以知道,这一条语句的定义理解为:我们定义了一个指向常量的指针p_1。

顶层const和底层const:

       顶层const:表示指针本身是个常量,定义可以延申。

       底层const:表示指针所指对象是一个常量,定义可以延申。

(4)const应用于函数

作为函数的形参:

1 //传递过来的参数在函数内不可以改变
2 void function(const int Var);
3 //参数指针所指内容为常量不可变
4 void function(const char* Var);
5 //参数指针本身为常量不可变
6 void function(char* const Var);
7 //引用参数在函数内不可以改变
8 void function(const int& Var); 

 

修饰函数返回值:

1 //函数返回值不能作为左值
2 const int func();
3 //把function()看作一个变量,那么指针所指地址的对应的内容不可改变
4 const int * function();
5 //把function1()看作一个变量,那么指针本身不可以改变
6 int * const function1();

 

函数的返回值不能作为左值这一点,在操作符重载中有所体现,比如说,避免了等号左边出现表达式的情况。const引用做形参和返回值在这里不再赘述,留下来自己思考!

(5)类成员中的const

修饰类的变量:

 1 class Test {
 2 public:
 3     //错误,const修饰的成员变量只能在初始化列表中赋初始值
 4     Test(int i)  {
 5         value = i;
 6 };
 7     //正确
 8     Test(int i) :value(i) {};
 9 private:
10     const int value;
11 };

 

修饰成员函数:

 

(6)

(7)

(8)

(9)

(10)

 

 

Guess you like

Origin www.cnblogs.com/Royzzzzz/p/10960656.html