【C++】:const限定符 作用于变量&指针

const限定符作用于变量

const放在变量前面称为const常量

const int MaxSize = 100 //定义一个常量
MaxSize = 44            //试图修改一个常量,系统会报错

const在文件中默认为局部变量

若想要在其他文件中使用这个const变量,则需要在定义的时候加上extern
Note: 非const变量默认是extern。因此不需要再变量前面添加extern

//file1.cpp
extern const int MAX_COUNT = 20
//file2.cpp
extern const int MAX_COUNT; //使用file1中的MAX_COUNT。

const限定符作用于指针

常量指针

特点:调用时const限定符在“*”前面,这种情况指针不能改变其指向对象的值,想要改变就要用下图中第三段中的const_cast去掉指针的常量性。

//============================================================================
// Name        : InsertSort.cpp
// Author      : YL
// Version     :
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    double pi = 3.14;
    double pi3 = pi*3.0;

    const double* cptr = &pi;
    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 = &pi;
    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 = &pi;
    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去掉指针的常量性。

//============================================================================
// Name        : InsertSort.cpp
// Author      : YL
// Version     :
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    double pi = 3.14;
    double pi3 = pi*3.0;

    double* const cptr = &pi;
    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 = &pi;
    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 = &pi;
    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

指针常量指针

//============================================================================
// Name        : InsertSort.cpp
// Author      : YL
// Version     :
//============================================================================
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    double pi = 3.14;
    double pi3 = pi*3.0;

    const double* const cptr = &pi;
    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 = &pi;
    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 = &pi;
    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;

const限定符作用于函数参变量

注意:我们经常会看到很多函数的形参都会用const限定符修饰,那么为什么那么多形参都用const修饰呢。原因在于,我们传参数都是会用引用或者指针。而在这之前加上const是用以指明使用这种参数仅仅只是为了效率(因为传址比传值效率高),而不是想让调用函数修改对象的值(因为传址的不好的地方就是会把原始的址对应的值改变掉)。

void g(const in& ref);
void strlen(const char* str);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanCruise

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值