Const usage

xiaoxiao2021-03-06  32

Const is mainly for the robust shape of the program, and the program is reduced.

The most basic usage:

Const int a = 100; B content is constant, B can only be 100, which is to declare a constant of an int type (#define b = 100)

INT const b = 100; // is the same as above

Const pointers and references are generally used in the parameters of the function

INT * m = & a; // error, constant can only use the normal pointer

INT c = 1; const INT * PC = & C; // Oxygen pointer can point to constants

Const int * pa = & a; / / The content points to the content is constant (that is, the value of B is constant)

INT const * a = & b; // The content pointing to the constant (that is, the value of B is constant) * p = 3 // error

INT * const a = & b; / / pointer is constant, can not change the pointer, such as A but can be changed * p = 3;

From this, it can be seen that the Const puts the contents of the pointer. Const is placed on the right side to modify the pointer.

itself.

Const reference usage and pointer

INT const & a = b; like a pointer

Const Int & A = B;

But without int & const a = b, the use cannot be shifted by reference, but just 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 const; // is a normal function, can only be used in the class, calling it objects cannot change the member value

Const int a (); // Returns constant, so you must call COSNT INT A = A ();

INT A; // Parameter cannot be changed, available in any function

INT A (const Int *);

....

Int height () 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 Int * 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 correct, but they are referenced and general reference

For Const Int & Ref1 = A; in fact, this REF1 has nothing to do with A.

The REF1 is actually a reference to a temporary amount. Similarly const INT & Ref2 = 1;

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 immediate use is only 1

Const I = 1;

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

COUT << i << endl;

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

At present, I will encounter these problems. Is that still addressed?

Can you talk about your experience too. Everyone exchanges exchange

This is what I found when I was wrong.

Int height () const; // often function can only be called by the normal function

INT MAX (int, int) const;

Int max = max (Height (), Height ());

Thinking Again In C (1) Constant principle Cphj (original)

Some places are very 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; // 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) You cannot pass the Const Type Community to the 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) You cannot return a value of the const type of object as a function of the Non-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; // 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 Int * 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 Int

{

Return 100;

}

INT * G (INT & RA) / / Equivalent to Return INT * Const

{

Return & ra;

}

Class Ctest

{

Int n;

PUBLIC:

CTEST (int N) {this-> n = n;

}

CTEST H () // The return is CTest

{

Return Ctest (200);

}

f () = 200; // Error

INT x = 100;

INT Y = 200;

INT * PX = & x;

g (y) = px; // error

* g (y) = x; // OK, from this point you can see that the g () returns not const *

Ctest T (100);

h () = t; // ok, but it is a complete error, 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 100

INT const b = 100; b must be int type, can not be other types?

The meaning of this 2 sentence should be the same, Thinking In C is said

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; // This four statements have occurred "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 ", speak in grammar Just b = 100.0; // 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 a;

A.Getdata (); // Call int & getdata () {return data;}

/ / But if there is no such function, you can also call Const Int & GetData () Const

Const a const_a; const_a.getdata (); // Call const Int & getdata () const {return data;}

The frequent function can only adjust the function, it is because of this reason.

you are vicious! Plus two points

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 correct, but they are referenced and general reference

For Const Int & Ref1 = A; in fact, this REF1 has nothing to do with A.

The REF1 is actually a reference to a temporary amount. Similarly const INT & Ref2 = 1;

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 immediate use is only 1

Const I = 1;

* (const_cast (& i)) = 2;

COUT << * (int *) & i << endl;

COUT << i << endl;

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

#include

Void fun (char b) {cout << "void" << endl;}

INT Fun (int const b) {cout << "int" << endl;}

int main ()

{

Fun (1.0); // Take a detailed look at the overload function.

Fun (4); // I want to call which one

Return 0;

}

I tried it, will I make an error? VC said: 'Fun': Ambiguous Call to overloaded Function

Add good, this is generally not paying attention.

Const I = 1;

* (const_cast (& i)) = 2;

COUT << * (int *) & i << endl;

COUT << i << endl;

This is really interesting, both of which are 2, can be compiled 2, 1

Const is always constant, this can be changed, otherwise it is contradictory.

The strange thing is the same as the PI and & I address, just like this is the optimization of compilation.

deal with

Const I = 1;

INT * pi = const_cast (& i);

* pi = 2;

Cout << * pi << Endl;

COUT << i << endl;

That is mainly implicit conversion

You can check the two functions in turn to see the call.

#include

// void fun (char b) {cout << "void" << endl;}

Void Fun (int b) {cout << "int" << endl;}

int main ()

{

Fun ('a');

Fun (4);

Return 0;

}

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

New Post(0)