First, the conclusion .
- The assignment of a non-reference type does not matter Const.
- Const references can point to const and non-Const. But non-const references can only point to non-Const.
- A pointer to a const that can point to non-Const. pointers to non-const, however, can only be pointed to non-Const.
Code explains everything
#include <iostream>#include<string>using namespacestd;//Test ConstintMain () {//------------Test Non-reference------------ intno=Ten; Const intNo2=no;//OK intNo3=no2;//ok! //It concludes that the assignment of a non-reference type does not matter the const //------------test References------------ int&noref=No; Const int&noref1=No;//int &no2ref=no2; Const int&no2ref1=NO2; int&no3ref=NO3; Const int&no3ref1=NO3; //It concludes thatconst references can point to const and non-Const. But non-const references can only point to non-Const. //------------The test pointer------------ int*pno=&No; Const int*pno_1=&no;// A pointer to a const that can point to a non-const //int *pno2=&no2;// Pointer to non-const, pointing only to non-const Const int*pno2_1=&NO2; int*pno3=&NO3; Const int*pno3_1=&NO3; //The above concludes: see Remarks return 0;}
C + + Const SUMMARY