Thinking Again In C ++ (1) Constant principle

zhaozj2021-02-16  64

Love the Thinking in series, so I played this name. The idea of ​​this article also comes to this book, or refers to comparison, or in-depth mining, or makes picking out, or has a feeling, including Thinking In C , and even including Thinking in Java.

Thinking Again In C (1) Constant principle

Keywords: C , constant, const, constant, reference, pointer, ginseng, argument, function, return value

1. Any object, reference, and pointer that cannot be modified as the value of Const modifications can be used as the left value of the assignment expression. Const int CX = 100; const INT & RCX = CX; const INT * PCX = & CX; CX = 200; // error RCX = 200; // error * PCX = 200; // error

2. The object of Const type cannot be referenced directly by the Non-Const type alias. (1) You cannot pass the Const type object to the Non-Const type reference. Const int CX = 100; INT & RX = CX; // Error (2) Do not pass the consocial parameters of the Const type to a function of the Non-Const type reference. Void f (int A) {} void g (int & ra) {} const INT CX = 100; F (cx); // ok g (cx); // error (3) Can't put a Const type object as Non -const type reference function return value. INT & F (Const Int & RCA) {Return Rca; // Error} INT X = 100; F (x);

3. You can reference the Non-Const object using the const type alias. The object cannot be modified by the const reference, but the object can be modified by the Non-Const reference. INT x = 100; int & rx = x; const INT & RCX = X; // ok x = 200; rx = 200; RCX = 200; // error

4. The properties of the pointer have two: the type of pointer and the constantity of the pointer itself. Among them, pointing to the Const object and pointing to the Non-Const object, it is a different pointer type. INT x = 100; const * pcx = & x; // [1] int * PX = & x; // [2] int y = 100; int * const cpy = & y; // [3] int * py = & y ; // [4] [1] [2] The type of two pointers is different; [3] [4] The constantity of the two pointers is different. The object is similar to the object and reference to the rules of the pointer to the object. That is, the Const type object cannot be directly indicated by the Non-Const type pointer; the pointer of the const type can be used to point to the Non-Const object (same 3). 5. You can assign the same type (including constant) constant value to the Non-Const pointer. INT x = 100; int * px; const INT * PCX = & x; px = pcx; // error int * const cpx = & x; px = cpx; // ok

6. If the return value of the function is a built-in type or a pointer, the return value automatically becomes the constant nature. However, the custom type is Non-Const property. INT f () // is equivalent to returning const {return 100;} int * g (int * g (int * const {return ra;} class ctest {Int n; public: ctest (int N) {this-> n = n;}}; ctest h () // is CTest {Return Ctest (200);}

f () = 200; // Error

INT x = 100; int * px = & x; g (y) = px; // error * g (y) = x; // ok, from this point you can see that g () returning is not Const Int *

CTest T (100); h () = t; // OK, but it is completely wrong, dangerous practice //, so the correct way to write h () is to return Const Ctest

转载请注明原文地址:https://www.9cbs.com/read-23825.html

New Post(0)