- 论坛徽章:
- 0
|
在C++ Primer第四版的178页上看到关于const的描述:
允许把非const对象的地址赋给指向const对象的指针,例如:
const double *cptr; //cptr may point to a double that is const
double dval = 3.14; //dval is a double;its value can be changed
cptr = &dval; //ok:but can't change dval through cptr
尽管dval不是const对象,但任何企图通过指针cptr修改其值的行为都会导致编译时的错误。
上面的是书上的内容,我给照抄下来的,但是我在做测试的时候,是可以通过指针cptr修改dval的值:例如:
dval = *cptr + 2;
请大家帮忙解释一下,我用的编译器是:
g++ (GCC) 4.6.1 20110626 (prerelease)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|