A few days ago, our QQ group made such a problem:
Excuse me:
INT ** Const A;
INT * const * a;
INT const ** a;
Did out?
I wrote some test programs as my understanding of this issue:
Program 1: About ** const ====================================== # include
COUT << ** PPA << Endl; getch (); / * ppa = & pb; // Unable to modify the value of the PPA, further explain the PPA is a constant cout << ** PPA; getch (); * / * ppa = & c ; // * PPA is COUT << ** PPA << Endl; getch ();
** PPA = 777; // ** PPA is the variable modified ** PPA is the value of COUT << "** PPA =" << ** PPA << Endl; cout << "c =" < Program 2: About * const * ====================================== # include INT main (int Argc, char * argv []) {INT A = 123; INT B = 456; INT C = 789; INT * PA = & a; int * Pb = & b; int * pc = & c; int ** PPT = & PC; int * const * ppa = & pa; // PPA initialization or not, it doesn't matter, the PPA is a variable COUT << "(long)" << (long) << (ppa) << endl; cout << "(* ppa)" << (long) (* ppa) << Endl; Cout < <"(** PPA) << (** ppa) << Endl; // If the PPA is not initialized, this statement will produce // life (at least in my machine) COUT << "(long) (& pa)" << (long) (& pa) << Endl; cout << "(long) (pa)" << (long) (pa) << endl; getch (); PPA = & PC; / * // Note: The following is to modify its value, not only to read its value * ppa = pc; // Regardless of whether the PPA is initialized, it always generates compilation error => Compiler prompt : Unable to modify a const object // This shows that * PPA is a constant, we can't modify the value of * PPA, regardless of whether PPA is initialized. // If the PPA is not initialized, the PPA and * PPA points to a random address, because the value of the * PPA cannot be changed, so it is safe to use PPA and * PPA (this is very interesting, maybe we can use this What is a bit? PPA = "<< ** PPA << Endl; cout <<" a = "<< a << endl; // modified ** PPA is the value of A. (); Return 0;} Program 3: About * const * ===================================== # include INT const d = 888; int const * pd = & d; int const ** ppd = & pd; INT const e = 888; int const * pe = & e; int const ** ppe = & pe; INT const ** PPA = PPD; / * This more interesting: PPA cannot initialize with & pa or ppc when defined, once the initialization code will have the following compilation error occurs, cannot communication 'int * *' to 'const Int * * ', But can initialize * / / * ppa = & pa; // even after the definition is also the same error * / PPA = PPE; // This sentence is that the PPA does not have a constant nature PPA can be changed // but can only point to two types of themselves. / * * PPA = PC; // This sentence will have "program access illegal address" error // so use is wrong. However, it is not a cash // interpreter error, which indicates that * PPA can be changed (although it is illegal) // instead of constant. // The analysis here seems to have problems or discussions * / * ppa = pe; // However, this sentence is feasible // pPC = PPE; // These two still have the same compilation error: PPC It is a constant nature that cannot change its value / * ** ppa = 777; // This sentence will have an error that cannot be modified the const object // This indicates that ** PPA is a constant nature * / getCH (); return 0;} summary : ============================ The amount modified by const is constant! The value of the amount cannot be changed. ** Const * Const * Const ** For example: const * p ==> * p cannot be changed, but Py's value is a const ** p ==> ** P cannot be changed, but the value of * p and p is possible to change * const *p ==> * p cannot be changed, but the value of p and ** p ==> p cannot be changed, but the value of * p and ** p can be changed. This Const seems to add a lock or a wall when it is "rewritten" to the object of the object, "who has been modified, who can't be assigned (compile error, of course Reading is allowed). Through this we can learn: const INT & AA :: Func (const INT & X) const; │ │ └ └ └ └ 全 成::: 不 └ 类 类 参 参: This function does not change the value of external parameter x Return Value: Call the code of the function cannot change the return reference value (otherwise we can use fun (a) = 12 // In fact, each of us has used this: cout <<<<. .. .) Through these, you can also be derived: Type const * const * const *! Try a try!