如下:
#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...

}
本文通过一个简单的C++示例,展示了即使使用const关键字也不一定能确保字符串对象的不可更改性,进而讨论了逻辑常量性的概念及其背后的原理。
如下:
#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...

}
491
1116
1130

被折叠的 条评论
为什么被折叠?