seeing the name of a thing one thinks of its function , Explain the problem well , Need to understand two parts :const Keywords and pointers .
const Basic knowledge of keywords
first , Explain what is const keyword .const Keywords are words constant Abbreviation of , Represents constant .const This keyword is used to decorate an explicit type
Variable of , Indicates that the value of the variable it modifies cannot be changed , Otherwise, an error will be reported .
that , What is display type ?
stay Visual Studio 2022 Enter the following code under :
int main() { const var; // Compiler error : Missing explicit type ( assume int) }
This is easy to understand ,const The purpose is to tell the compiler , The following variable is a constant , and C++ As “ Strongly typed language ”, Any one “ amount ” Must have a basic data type (int,
double these ), What is said here “ Missing explicit type ” It can be understood as the lack of basic data types . therefore , The above code should be modified as follows .
int main() { const int var; // This is just to illustrate the problem , Not initialized , The actual operation must be initialized at the time of definition // finish writing sth. int
const var; it's fine too , But not recommended }
Such a definition is actually telling the compiler , variable var First, there is a basic type int, The second is the immutable constant , After understanding this , Can explain the tape const Pointer to keyword . The commented writing method first tells the compiler , variable var The first is a constant , subsequently , The compiler wants to know what data type this constant is ( That is, how to parse the data in this variable ), Then the compiler finds int, Problem solving . generally speaking , It's better to use the former way ( Personally, I think the reason may be eye-catching ㄟ(≧◇≦)ㄏ). There is another problem that needs attention :
Constants must be initialized at definition time , This is just to illustrate the problem without initializing in the code , Pay attention to the actual compilation , The uninitialized part in the following text is the same if it is not explained .
Basic knowledge of pointer part
then , Let's explain the pointer part briefly . Pointer type is a quite special type , Its essence is still data
, This data represents the location of an area in memory . In the memory area indicated by the pointer , There is a basic type of data stored . For a pointer , It needs to complete two basic tasks :
first , Tell the compiler the location of the data ; second , Tell the compiler when you need to extract data from this location , What kind of reading method should be adopted
( For example, the data is integer , Just use the integer access method ), These are the two elements that make up the pointer . final , When defining pointer type variables , Written in the following form .
int main() { int* p; // The access mode of data in the memory area is int,p Is a variable that stores pointer data }
No matter how the data is accessed (int Good ,double Forget it ), Any pointer type variable stores a fixed length integer value (32 Occupied in bit 4 Bytes ,64 Occupied in bit 8 Bytes ), This value represents the storage location of the data , Generally speaking, it is the address number . last , Formed a system called “int*” Type of . Pay attention here ,
“int*” Is a new data type ,int It only represents the access method of the content pointed to by the pointer , Other images “float*”“double*” It only represents the different access methods . In definition ,
“*” Will be bound to the variable name first , Tell the compiler this is a pointer , Then it is combined with specific access methods to form a complete pointer type
. Investigate its essence , They are all shaped like “0x00AB” It's just an integer address number , The length is fixed . The best example of this is void Pointer to type ,void The pointer temporarily shelves the access problem of variables , Solve the more important address problem first . about void Type pointer , Unable to parse , Otherwise, an error will be reported , See the following example .
#include<iostream>//C++ //#include<stdio.h> //C language int main() { int a = 0;
void* p = &a;//p Integer number stored in a Address of , But it didn't tell the compiler how to access it
/* Errors will be reported in the following two ways , The reason is that the compiler doesn't know what method to extract p Data in address */ //cout << *p << endl; //C++, report errors
//printf("%d\n", *p);//C language , report errors /* The correct way to write it is to use forced type conversion to tell the compiler the access method */ cout << *(int*)p <<
endl; //C++ printf("%d\n", *(int*)p); //C language }
belt const Pointer analysis of
So much has been paved , Now we can explain the tape const There is a problem with the pointer of the keyword , Start with the simplest case , Directly give the following code , Then make a detailed analysis .
int main() { const int* var; //int const* var; it's fine too , But not recommended }
We already know “const data type Variable name ” You can define a constant variable whose value cannot be modified ,“ data type * Variable name ” You can define a variable of pointer type . We should pay attention to the problems mentioned before :
const Must combine a basic data type , Then a constant type of basic data type can be formed .
Here ,“*” First with var combination , Tell compiler variables var Is a pointer , To get real data , You need to find its location through the address first . After knowing this, the compiler also needs to confirm the access mode of the real data , therefore int combination const Tell the compiler
Pointer var The real data behind it is an immutable integer value .
Here is another point to note : In the above definition , variable var Initialization is not required , The reason is that var Itself has not been const modification ( What is decorated is the data it points to ).
Change order , How to understand it written like this ?
int main() { int* const var; }
Come here , You might ask ,const Must combine a basic data type , Which is the basic data type ? actually , This is what I gave at the beginning "int
const var" The purpose of this way of definition . under these circumstances ,var Combine first const Tell the compiler ,var This variable is not modifiable . Then the compiler will
Continue to confirm the basic data type of this immutable variable
, therefore ,“*” Continue to combine variable names var, Now the compiler knows ,var Is a pointer type , This pointer cannot be changed . After confirming this , The compiler wants to know this special , How should the immutable pointer be parsed ( At least tell it that it doesn't know for the time being ——void, It has been confirmed here yes int), Look ahead , This variable is also int modification , Finally, the compiler gets the information about variables var All the information of .
last , To sum up , Give the following advanced form of code , The basic analysis ideas have been annotated completely , There should be no problem in understanding .
int main() { //const var; Tell the compiler , Define a variable var, The value of this variable cannot be changed
/*----- Yes const, Compiler wants to know var What type is it -----*/ //* const var; Tell the compiler , The basic type of this constant is pointer type
/*----- The compiler wants to know how this pointer parses to get the actual data -----*/ //const int* const
var; Tell the compiler , Parse actual data with constant integer parsing method /*----- Final form -----*/ const int* const var;
/*----- It can also be written like this , But not recommended -----*/ //int const* const var; }
With the above analysis , It is not difficult to make the following summary .
For the first way of definition ,var The address saved by this pointer is variable , And the story data behind it , Because it is const int type , Cannot be modified .
int main() { int test_1 = 10; int test_2 = 20; const int* var =
&test_1;// Stored address variable , The actual value represented behind the address is immutable var = &test_2; // correct , At this time, the pointer points to test_2 //*var =
test_1; // error }
For the second way of definition ,var The pointer itself is const modification , The saved address is immutable , But the actual data behind this address can be modified .
int main() { int test_1 = 10; int test_2 = 20; int* const var = &test_1; //var
= &test_2; // error ,var The address in is const type , Cannot modify *var = test_2; // correct ,var The actual data pointed to can be changed }
For the third definition method ,var The pointer itself is const modification , The saved address cannot be changed . meanwhile , The actual data pointed to by the address is also const modification , Also cannot be modified .
int main() { int test_1 = 10; int test_2 = 20; const int* const var = &test_1;
//var = &test_2; // error //*var = test_2; // error }
Write at the end
1. stay int main(){} In function body , If missing return sentence , The compiler automatically populates .
If we don't place an explicit return statement at the end of main(), a return
0; statement is inserted automatically. In the program examples in this book, I
do not place an explicitreturn statement.
— Essential C++ by Stanley B. Lippman
2. The understanding of pointer and modifier priority is the key , Especially the pointer . Personally, I think after understanding this , understand C++ References in will also be much easier .
3. Limited by the author's beginnings C/C++ , Limited level , If there is something wrong, I hope all interested people will not hesitate to correct it .
Technology