const对象一样可能被更改

来源:互联网 发布:mysql登录 编辑:程序博客网 时间:2024/05/28 19:24

如下:

#include <string>
#include 
<iostream>
using namespace std;
void main(){
    string s(
"Hello");
    
char* p=&s[0];
    
*p='J';
    cout
<<s.c_str()<<endl;
    
//可见,这种情况说明了,即使用const也未必能保证对象的不可更改!
    
//这种情况导出所谓的logical constness...


}
原创粉丝点击