Const keyword in c + +, can be used to modify any of the scope of the variable, function parameters, function ontology, function returns a value, member functions, iterators, and pointer can also be used to modify the pointer itself, is a versatile, we want to learn more about its internal details, as well as the logical mystery, let this multi-functional Swiss army knife to play its role, And don’t cut yourself!
1. Define a constant global variable:
const int global = 100; // No more assignment after initialization
Such a global is actually a constant, which is one of C++’s alternatives to macro definitions. Const constants have type detection to make the compiler more efficient.
2. Define a constant pointer. There are two versions of this:
const int *p1 = &a; // P1 cannot modify the target it points to
int const *p1 = &a; // another equivalent form of p1
int *const p2 = &a; // P2 itself cannot be modified
Copy the code
P1 in the above code is often used as a function parameter to restrict the permissions of Pointers, which is a security benefit. P2 is rarely used, and we rarely need a pointer that itself points to immutable
3. Define a constant iterator for STL, which has two similar versions:
vector::const_iterator it1; // IT1 cannot modify the target object it refers to
const vector::iterator it2; // IT2 itself cannot be modified
Since iterators are really generalized Pointers, IT1 is actually p1 and IT2 is p2 of the above code. Similarly, IT1 is used more often to limit the permission of iterators.
4. Define the internal constant members of a class, including constant member data and methods:
class text
{
const int aconst = 100; // Constant member data must be initialized
void func(void) const; // A constant member method that can only be called by a constant object
static int astatic;
};
It’s easy to understand that the class’s constant member data, aconst, means that the class object cannot modify it. Func is a bit of a puzzle. Func cannot change the bit of any class object. This is what the C++ standard defines as const member methods. \
Take a closer look at the use of func methods:
int text::astatic = 100; // Initialize static data out of class
Void text::func(void) const // Defines const member methods outside the class
{
astatic = 200; // Astatic is not object data and can be modified here
aconst = 200; / / error! Const functions cannot modify class object data
}
Defining the above member method is important because it clearly tells the consumer of the class which functions can modify class object information and which do not.
Const member methods of functions such as func() can only be called by regular objects to ensure that permission tightening rules are not violated, such as:
text t1; // Common object T1
const text t2; // const t2
t1.func(); / / error! A regular object cannot call a constant member method
t2.func(); There is nothing wrong with / /
In other words, if the above class text provides a non-const version of the func function, t1 will automatically call that non-const version.
The above statement may seem mundane, but it might be interesting to consider that the constant members of the bit-wise feature specified by C++ syntax handle Pointers to class members that point to memory outside the class.
5. Common features of bit-wise and logical-wise
A bit-wise constness is a read-only constraint in the internal memory sense of a class object, while a loigcal-wise is a read-only constraint in the logical sense. The compiler is not intelligent. It can only implement constraints in the bit-wise sense. The following example discusses the behavior of const operator[] :
class text
{. .char &operator[ ](int pos) const;
private:
char *p;
};
// The const keyword guarantees that the member method can only be called by const objects
char &text::operator[ ](int pos) const
{
return p[pos];
}
Copy the code
In the above code, the real string is stored in the memory region to which the pointer p points. If we define a const constant object, this will only protect the internal information of the class (p itself) from modification, but not the memory region to which it points. Take a look at the following code:
const text ct(“abcd”);
ct[0] = ‘A’; // a statement that is allowed by the compiler but contradicts logic
Ct is a const type object here, starting from the literal understanding, we should think that ct is a constant object, we should not modify the internal information by any means, but unfortunately, the inside of the ct string information in fact does not exist in the kind of memory area, and then appeared a const type object can be interesting phenomena of the assignment.
We can rewrite the return type of the operator[] member method:
const char &operator[ ](int pos) const;
Ct [0] can no longer be assigned, but this is a case in point. The key thing to know is: If our class object has a member pointer, then a const member method in general guarantees only the constantiness of bits-wise (that is, the memory information inside the class object is not modified), but not the constantiness of so-called logical-wise (that is, what information is logically not modified).
We’re not done talking about Const, he’s a versatile player, and we’ll have a chance to explore more.