1.作用于变量
防止变量被修改。
const int MaxSize = 100 //定义一个常量
MaxSize = 44 //试图修改一个常量,系统会报错
2.作用于指针
常量指针
特点:调用时const限定符在“*”前面,这种情况指针不能改变其指向对象的值,想要改变就要用下图中第三段代码中的const_cast去掉指针的常量性。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* cptr = π
cout << "the cptr value = "<< *cptr <<endl;
*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
cptr = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//..\src\InsertSort.cpp:17:10: error: assignment of read-only location '* cptr'
// *cptr = 5.0;
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* cptr = π
cout << "the cptr value = "<< *cptr <<endl;
//*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
cptr = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//the cptr value = 3.14
//the const_cast cptr value = 3.140000
//the cptr value = 9.420000
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* cptr = π
cout << "the cptr value = "<< *cptr <<endl;
//*cptr = 5.0;
*(const_cast<double*>(cptr)) = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
cptr = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//the cptr value = 3.14
//the const_cast cptr value = 5.000000
//the cptr value = 9.420000
指针常量
特点:调用时const限定符在“*”后面,这种情况指针不能改变其指向,想要改变就要用下图中第三段中的const_cast去掉指针的常量性。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
cptr = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//..\src\InsertSort.cpp:20:10: error: assignment of read-only variable 'cptr'
// cptr = &pi3;
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
//cptr = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//the cptr value = 3.14
//the const_cast cptr value = 5.000000
//the cptr value = 5.000000
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
//cptr = &pi3;
const_cast<double*>(cptr) = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//the cptr value = 3.14
//the const_cast cptr value = 5.000000
//the cptr value = 9.420000
指针常量指针
常量指针和指针常量的结合体。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
*cptr = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
// cptr = &pi3;
// printf("the cptr value = %f\n",*cptr);
return 0;
}
//..\src\InsertSort.cpp:17:10: error: assignment of read-only location '*(const double*)cptr'
// *cptr = 5.0;
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
//*cptr = 5.0;
*(const_cast<double*>(cptr)) = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
// cptr = &pi3;
// printf("the cptr value = %f\n",*cptr);
return 0;
}
//the cptr value = 3.14
//the const_cast cptr value = 5.000000
#include <iostream>
#include <vector>
using namespace std;
int main()
{
double pi = 3.14;
double pi3 = pi*3.0;
const double* const cptr = π
cout << "the cptr value = "<< *cptr <<endl;
//*cptr = 5.0;
*(const_cast<double*>(cptr)) = 5.0;
printf("the const_cast cptr value = %f\n", *cptr);
//cptr = &pi3;
const_cast<double*>(cptr) = &pi3;
printf("the cptr value = %f\n",*cptr);
return 0;
}
//只有这种情况跟之前所有的情况都不一样,没办法取出这种情况下的const特性
//..\src\InsertSort.cpp:71:10: error: assignment of read-only variable 'cptr'
// cptr = &pi3;
3.作用于形参
我们经常会看到很多函数的形参都会用const限定符修饰,那么为什么那么多形参都用const修饰呢。原因在于,我们传参数都是会用引用或者指针。而在这之前加上const是用以指明使用这种参数仅仅只是为了效率(因为传址比传值效率高),而不是想让调用函数修改对象的值(因为传址的不好的地方就是会把原始的址对应的值改变掉)。
void g(const in& ref);
void strlen(const char* str);
4.在文件中默认为局部变量
若想要在其他文件中使用这个const变量,则需要在定义的时候加上extern 。需要注意的是非const变量默认是extern。因此不需要再变量前面添加extern。
//file1.cpp
extern const int MAX_COUNT = 20
//file2.cpp
extern const int MAX_COUNT; //使用file1中的MAX_COUNT。
网友评论