diff --git a/english/basic_content/const/README.md b/english/basic_content/const/README.md new file mode 100644 index 0000000..f324abc --- /dev/null +++ b/english/basic_content/const/README.md @@ -0,0 +1,382 @@ + + + +## 1.The Definition Of const + +Const type is that people use type specifier **const** signiture type to demonstrate,const variables or objects can not be updated。 + +## 2. Effect Of Const + +(1)define variable + +``` +const int a=100; +``` + +(2) check type + +The difference between const variable and define variable: ~~**Constants have types, which can be checked by the compiler; the define macro definition has no data type, it is just a simple string replacement, and cannot be checked for security +**~~ Thanks for point this bug out. +> https://github.com/Light-City/CPlusPlusThings/issues/5 + +Only variables of type integer or enumeration are defined by `const`. In other cases, it is only a 'const' qualified variable and should not be confused with constants. + +(3)prevent modification, protect and increase program robustness + + +``` +void f(const int i){ + i++; //error! +} +``` +(4)Save space and avoid unnecessary memory allocation + +From the compile point of view, the variables can be defined by const only give the corresponding memory address. Meanwhile the #define gives an immediate number. + +## 3.const object is the file local variable by default + + +

Attention:not const will be set as extern by default.For the const variable to be accessible in other files, it must be explicitly specified as extern in the file + +

+> Access of variables not modified by const in different files + + +``` +// file1.cpp +int ext +// file2.cpp +#include +/** + * by 光城 + * compile: g++ -o file file2.cpp file1.cpp + * execute: ./file + */ +extern int ext; +int main(){ + std::cout<<(ext+10)< +using namespace std; +int main(){ + + int num=0; + int * const ptr=# //const指针必须初始化!且const指针的值不能修改 + int * t = # + *t = 1; + cout<<*ptr< +using namespace std; +int main(){ + const int num=0; + int * const ptr=# //error! const int* -> int* + cout<<*ptr< +#include"apple.cpp" +using namespace std; + +Apple::Apple(int i):apple_number(i) +{ + +} +int Apple::add(int num){ + take(num); +} +int Apple::add(int num) const{ + take(num); +} +void Apple::take(int num) const +{ + cout<<"take func "< +#include"apple.cpp" +using namespace std; +Apple::Apple(int i) +{ + +} +int Apple::add(int num){ + take(num); +} +int Apple::add(int num) const{ + take(num); +} +void Apple::take(int num) const +{ + cout<<"take func "< +#include"apple.cpp" +using namespace std; + +Apple::Apple(int i):apple_number(i) +{ + +} +int Apple::add(int num){ + take(num); +} +int Apple::add(int num) const{ + take(num); +} +void Apple::take(int num) const +{ + cout<<"take func "< +#include"apple.cpp" +using namespace std; +const int Apple::apple_number=10; +Apple::Apple(int i) +{ + +} +int Apple::add(int num){ + take(num); +} +int Apple::add(int num) const{ + take(num); +} +void Apple::take(int num) const +{ + cout<<"take func "< +#include"apple.cpp" +using namespace std; +const int Apple::apple_number=10; +int Apple::ap=666; +Apple::Apple(int i) +{ + +} +int Apple::add(int num){ + take(num); +} +int Apple::add(int num) const{ + take(num); +} +void Apple::take(int num) const +{ + cout<<"take func "< +using namespace std; + +void f(const int i){ + i=10; // error: assignment of read-only parameter ‘i’ + cout< +using namespace std; +int main(){ + const int b = 10; + b = 0; //error + const string s = "helloworld"; + const int i,j=0; +} diff --git a/english/basic_content/const/extern_const/const_file1.cpp b/english/basic_content/const/extern_const/const_file1.cpp new file mode 100644 index 0000000..7077ac9 --- /dev/null +++ b/english/basic_content/const/extern_const/const_file1.cpp @@ -0,0 +1 @@ +extern const int ext=12; diff --git a/english/basic_content/const/extern_const/const_file2.cpp b/english/basic_content/const/extern_const/const_file2.cpp new file mode 100644 index 0000000..53729f4 --- /dev/null +++ b/english/basic_content/const/extern_const/const_file2.cpp @@ -0,0 +1,11 @@ +#include +/** + * by 光城 + * compile: g++ -o file const_file2.cpp const_file1.cpp + * execute: ./file + */ +extern const int ext; +int main(){ + + std::cout< +/** + * by 光城 + * compile: g++ -o file file2.cpp file1.cpp + * execute: ./file + */ +extern int ext; +int main(){ + + std::cout<<(ext+10)< +using namespace std; + + +int main(){ + const int *ptr; + *ptr=10; //error +} diff --git a/english/basic_content/const/funciton_const/condition1/condition2.cpp b/english/basic_content/const/funciton_const/condition1/condition2.cpp new file mode 100644 index 0000000..9db7339 --- /dev/null +++ b/english/basic_content/const/funciton_const/condition1/condition2.cpp @@ -0,0 +1,9 @@ +#include +using namespace std; + + +int main(){ + const int p = 10; + const void *vp = &p; + void *vp = &p; //error +} diff --git a/english/basic_content/const/funciton_const/condition1/condition3.cpp b/english/basic_content/const/funciton_const/condition1/condition3.cpp new file mode 100644 index 0000000..5809e5f --- /dev/null +++ b/english/basic_content/const/funciton_const/condition1/condition3.cpp @@ -0,0 +1,12 @@ +#include +using namespace std; + +int main(){ + const int *ptr; + int val = 3; + ptr = &val; //ok + int *ptr1 = &val; + *ptr1=4; + cout<<*ptr< +using namespace std; +int main(){ + + int num=0; + int * const ptr=# //const指针必须初始化!且const指针的值不能修改 + int * t = # + *t = 1; + cout<<*ptr< +using namespace std; +int main(){ + + const int num=0; + int * const ptr=# //error! const int* -> int* + cout<<*ptr< +using namespace std; +int main(){ + + const int num=10; + const int * const ptr=# //error! const int* -> int* + cout<<*ptr< +using namespace std; + +int main(){ + + const int p = 3; + const int * const ptr = &p; + cout<<*ptr<