Const usage

xiaoxiao2021-03-06  37

Const usage

Const is mainly for the robust, reducing program error. The most basic usage: const INT A = 100; B content constant, B can only be 100 is also a constant of an int type (#define b = 100) INT const b = 100; // is the same as above

Const pointer and reference generally use int * m = & a; // error, constant can only be used with normal pointer INT c = 1; const * pc = & c; // normally pointer can point to constants

Const Int * PA = & A; / / The content points to the constant (that is, the value of B is constant) INT const * a = & b; / / The content points to the content (that is, the value of B is constant) * p = 3 // Errorint * const a = & b; / / pointer is constant, cannot change the pointer, such as A but can be changed * p = 3;

From this, it can be seen that the Const put on the left side modified is the content of the pointer. Const is placed on the right side of the pointer itself.

The usage and pointer of Const reference is int condition & a = b; and the pointer is the same int & a = B; and the pointer is the same but there is no int & const a = b, but the use of the int & const a = b is because the reference cannot be shifted, but it is just a WARNING.

Const Int * const a = & b; // integrated application, generally used to deliver multi-dimensional arrays such as: char * init [] = {"paris", "in the", "spring"}; void fun (const * Const a) {} fun (init) // protection parameters are not modified

INT a (int) const; is a normal function, only in the class, calling it objects cannot change the member value const I (); // Returns constant, so you must call COSNT INT A = A (); int A; // Parameter cannot be changed, can be used in any function int A; .... int.com () const; // often function can only be called by the normal function INT MAX (INT, INT) const; int max = max (Height (), Height ());

Const int * pheap = new int; delete pheap; p = null; // Error my solution is to force type conversion const * pheap = new int (1); delete (int *) PHEAP; PHEAP = NULL;

I should pay attention when const and reference are used.

Const int a = 1; const INT & REF1 = A; const INT & Ref2 = 1;

REF1 and REF2 are all correct, but the contents and general references are different to const Int & ref1 = a; in fact, this REF1 has not had any relationship with the ref1 is actually a temporary reference. Similarly const INT & REF2 = 1; is also a reference to a temporary amount. When the reference temporary amount is the implicit type conversion of C , it can work. The survival of the temporary survival is the same.

Second, strong CONST objects may lead to unfained behaviors

For optimized compilers, code const I = 1; When the variable I is used later, the compiler will optimize the access to I, and the only number of direct use 1

Const I = 1;

* (const_cast (& I)) = 2; cout << * (int *) & i << endl; cout << i << Endl; so const_cast, const_cast, const_cast, can cause no definition behavior, I will When you encounter these problems, is that I have added?

Can you talk about your experience too. Everyone exchanges this is the int Height () const; // automatic function that I found during the wrong mistake, INT MAX (Int, int) constant; int max = max (Height (), Height ());

Thinking Again In C (1) Constant principle Cphj (original) Some places are inspired

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; // errorrcx = 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); // okg (cx); // error (3) Can't use the Const type object as Non- The function returns the value of the const type reference. 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; // okx = 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; // Errorint * 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

Const int b = 100; B content is constant, B can only be 100INT const b = 100; b must be int type, can not be other types? This 2 sentence should be the same, Thinking In C is like this Say

Const int a = 100; the content of A is constant, and a can only be 100 (the same cannot be converted). INT const b = 100; b must be int type, cannot be other types? (The same cannot be modified in use). Therefore, A and B are the same, called integer constants, cannot be modified in use, of course, can not be converted to other types. #include

Using namespace std;

INT main () {const INT A = 100; int const b = 100;

A = 100; // These four statements will appear "Cannot Modify a const object b = 100; // in function main ()" error prompt, that is, any attempt to modify A = 100.0; // a and B (actually the same) behavior will appear "disaster", say B = 100.0 in grammar; // is A and B can't appear on the left of the assignment statement!

COUT << '/ n' << a << '/ n' << b << endl;

Return 0;}

The call to the normal function is such that constant objects can only call the constant member function, and you can adjust the member function, you can also adjust the general member function, but when a function has const and non-constant two versions, Const Object calls CONST version, non-Const object transfer non-const version example: class a {public: int & getdata () {return data;} const INT & getData () const {return data;} private: int data;} a; A.GetData (); // Call int & getdata () {return data;} // But if there is no this function, you can also call const_a; const_a.getdata (); // call Const Int & getData () const {return data;} The normal function can only adjust the function, it is because of this reason.

you are vicious! Plus two points

First, const and references should pay attention to const I = 1; const INT & REF1 = A; const INT & REF2 = 1;

REF1 and REF2 are all correct, but the contents and general references are different to const Int & ref1 = a; in fact, this REF1 has not had any relationship with the ref1 is actually a temporary reference. Similarly const INT & REF2 = 1; is also a reference to a temporary amount. When the reference temporary amount is the implicit type conversion of C , it can work. The survival of the temporary survival is the same.

Second, strong CONST objects may lead to unfained behaviors

For optimized compilers, code const I = 1; When the variable I is used later, the compiler will optimize the access to I, and the only number of direct use 1

Const I = 1;

* (const_cast ) = 2; cout << * (int *) & i << endl; cout << i << end1

So, do const_cast for Const objects can lead to unfained behaviors

#include void fun (char b) {cout << "void" << endl;} int full (int const b) {cout << "int" << endl;} int main () {FUN (1.0); // Take a detailed look at the heavy-duty function, FUN (4); // I want to call which one

Return 0;} I tried it, will it be wrong? VC said: 'Fun': Ambiguous Call to overloaded FUNCTION

Good, this generally does not pay attention to const I = 1; * (const_cast (& I)) = 2; cout << * (int *) & i << endl; cout << i << Endl; this is really interesting, two are 2, which can be compiled is 2, 1 constant is constant, which can be changed, otherwise it is the contradiction, the PI and & I address is the same. Ah, just like this is the above-up to compile optimization processing const I = 1; int * pi = const_cast (& i); * pi = 2; cout << * pi << endl; cout <

That is mainly implicit conversion you can check two functions to check out #include // void fun (char b) {cout << "void" << endl;} void fun (int B) ) {cout << "int" <<} int main () {fun ('a'); fun (4); return 0;}

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

New Post(0)