In addition to constants, const has many other purposes. Use const to improveProgramThis article lists some common const usage and references C ++ FAQs lite.
1. Use const to modify function parameters
Modifying function parameters with const can prevent accidental modification of parameters in the function body.
Void F1 (const STD: string & S );// Const reference Transfer
Void F2 (const STD: string * sptr );// Const pointer Transfer
Void F3 (STD: String S );// Value transfer
For the first two types, if you modify the parameter content in the function body, an error will be reported during compilation.
For the last type, the function body generates a copy of the object, which can be modified. However, after leaving the function body, the copy is automatically destroyed, the modifications do not affect the passed parameters.
2. if the function parameter is not of the const type, you cannot directly pass in a const type parameter. You must first make a local copy of the non-const type and then pass the copy as a parameter to the function.
Void G1 (STD: string & S );
Void F1 (const STD: string & S)
{
G1 (s );// Error
STD: String localcopy = s;
G1 (localcopy );// Correct
}
3. Differences between const int *, int const *, int * const, const int * const, and INT const * const
The best difference is to read the variable declaration from the back, as shown in the following English after the comment:
Int x = 10;
Const int * P1 = & X; // P1 pointsA integer that is const
// The value of X cannot be changed through P1, that is, * P1 = 5 will produce a compilation error.
Int const * P2 = & X;// P2 pointsA const integer
// The value of X cannot be changed through P2, that is, * P2 = 5 will generate a compilation error, which is equivalent to P1.
Int * const P3 = & X; // P3 isA const pointerTo a integer
// The value of P3 cannot be changed, that is, P3 ++ will generate a compilation error.
Const int * const P4 = & X; // P4 isA const pointerToA integer that is const
// The P4 value cannot be changed. That is, P4 ++ will generate a compilation error.
// The value of X cannot be changed through P4, that is, * P4 = 5 will produce a compilation error.
Int const * const P5 = & X;// P5 isA const pointerToA const integer
// The P5 value cannot be changed, that is, P5 ++ will generate a compilation error.
// The value of X cannot be changed through P5, that is, * P5 = 5 will generate a compilation error, which is equivalent to P4.
4. Differences between const Int &, int const &, Int & Const
The difference is the same as 3:
Int x = 10;
Const Int & X1 = X; // X1 aliasesA integer that is const
// The value of X cannot be changed through X1, that is, X1 = 5 will generate a compilation error.
Int const & X2 = X; // X2 aliasesA const integer
// The value of X cannot be changed through x2, that is, X2 = 5 will generate a compilation error.
Int & Const X3 = X; // X3 is a const alias to a INTEGER (Make no sense)
// Because the reference itself is of the "const" type, once the referenced object is specified, it cannot be changed,
// Therefore, the const is meaningless. It is equivalent to Int & X3 = X;
5. Const member functions
If you add a const after the parameter table of the member function, it means that the function will not perform the * This object operation, that is, the value of the member variable will not be modified.
Class Fred
{
Public:
Void inspect () const; // This function will not be modified * This
Void mutate (); // This function may be modified * This
};
Void usercode (Fred & changeable, const Fred & unchangeable)
{
Changeable. Inspect (); // Correct
Changeable. mutate (); // Correct
Unchangeable. Inspect ();// Correct. The content of * this object will not be changed
Unchangeable. mutate ();// Compilation error: unchangeable references a const object,
// The content of the const object cannot be changed through this reference
}
6. If a const member function returns a member of this object through reference, it should use constant reference:
Class person
{
Public:
STD: String STR;
Const STD: string & name_good () const
{
Return STR; // Correct: although the function caller obtains the reference of STR, the member cannot be changed because it is of the const type.
}
STD: string & name_edevil () const
{
Return STR; // Compilation error: the function caller has the opportunity to change Str
}
};
For example:
Void mycode (const person & P) // Promise not to modify the person object through the const keyword
{
P. name_edevil () = "Igor "; // But it can be modified in this way! (Because the returned object is not a const object)
}
7. Const overload
The so-called const overload means that the declarations of two functions or operators are identical, but one of them has a const modifier.
Class Fred
{
Public:
Int X;
Void fun () const
{
Cout <"const fun" <Endl;
}
Void fun ()
{
Cout <"non-const fun" <Endl;
}
};
Void mycode (Fred & F1, const Fred & F2)
{
F1.fun (); // Output non-const fun
F2.fun (); // Output const fun
}
For a const object, call a function of the const version. For a non-const object, call a function of the non-const version.
This function is called if only functions of the const version are available. If only functions of the non-const version are available, a compilation error is generated for the const object.
In addition, if you want to write a container class, you need to overload the subscript operator []. Subscript operators must be overloaded by const ( Come in pairs ):
Class Fred {...};
Class myfredlist {
Public:
Const Fred & operator [] (unsigned index) const;
Fred & operator [] (unsigned index );
...
};
Why are functions of these two versions provided? Because the const version needs to be called in the const member function, rather than the non-const version needs to be called in the non-const member function.
8. Use the const member function to make some externally invisible changes to the data member.
Const member functions generally do not need to change the value of data members. However, in some cases, you need to make some externally invisible changes, for example, the set object may need to cache the information of the object it last searched for to improve the next search efficiency. There are two implementation methods:
(1) modify the data member with the mutable keyword before modification. (vc6.0 supports this keyword)
(2) If the compiler does not support this keyword, You need to perform const transformation in the const member function:
Void set: Lookup () const
{
Set * Self = const_cast <Set *> (this );// Remove the const feature of this pointer, so that you can modify the object data member.
...
}
9. Changing the value of a variable through the const int * pointer does not mean that the value of the variable cannot be changed: only the pointer promises not to change the value of the variable, rather than the variable promises not to change itself.
Void F (const int * P1, int * P2)
{
Int I = * P1; // Obtain the original * P1 Value
* P2 = 7; // If p1 = P2, the * P1 value will also be changed
Int J = * P1; // Obtain the new * P1 Value
If (I! = J ){
STD: cout <"* P1 changed, but the P1 pointer was not passed! \ N ";
Assert (p1 = P2 ); // This is I! = The unique possibility of J
}
}
Int main ()
{
Int x = 5;
F (& X, & X );// Pass the same pointer
Return 0;
}
10. You cannot convert Foo ** to const Foo **. You can convert it to const Foo * const *
Class Foo {/*...*/};
Void F (const Foo ** P );
Void g (const Foo * const * P );
Int main ()
{
Foo ** P = /*...*/;
...
F (p ); // Compilation error: cannot convert Foo ** to const Foo **
G (P ); // Correct: You can convert Foo ** to const Foo * const *
...
}
Why is this restriction required? You can see the following example:
Class Foo {
Public:
Void modify ();// This function can be used to modify objects.
};
Int main ()
{
Const Foo X;
Foo * P;
Const Foo ** q = & P; // Assume that you can convert Foo ** to const Foo **(In fact, this line of statements produces a compilation error.)
* Q = & X; // P points to X
P-> modify (); // Use P to modify the content of X! X is a const object!
...
}