c++ const 类型检查

3)防止修改,起保护作用,增加程序健壮性
#include<iostream>
using namespace std;

void f( const int i){
    i=10; // error: assignment of read-only parameter ‘i’
    cout<<i<<endl;
}

int main(){
    f(1);
}

可运行
#include<iostream>
using namespace std;

void f(  int i){
    i=10; // error: assignment of read-only parameter ‘i’
    cout<<i<<endl;
}

int main(){
    f(1);
}

posted @ 2022-08-19 22:48  luoganttcc  阅读(6)  评论(0)    收藏  举报