成员函数的const不能被修改,包括指针

#include <iostream>

class A
{
private:
    std::string a;
public:
    A(std::string b) :a(b){}
    const char& operator[](int b)const //两个const都不能少
    {
        return a[b];
    }
};
int main()
{
    A a("hello");
  //a[0]='j'; 不能
char*p = &a[0];
  *p = 'j'; 也不能,编译器信息:“return”: 无法从“const char”转换为“char &” std::cout
<< *p; }
原文地址:https://www.cnblogs.com/vhyc/p/5574821.html