小编Kin*_*g-J的帖子

关于c ++中的关键字"const"

根据c ++语法,const int* const p意味着p指向它并且它的值不能被重写.但是今天我发现如果我这样编码:

void f(const int* const p)  
 { 
     char* ch = (char*) p;
     int* q = (int*) ch;
     (*q) = 3;  //I can modify the integer that p points to
 }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,关键字"const"将失去它的效果.使用"const"有什么意义吗?

c++

3
推荐指数
1
解决办法
102
查看次数

标签 统计

c++ ×1