成员函数的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; }

 

posted on 2016-06-11 10:20  Kooing  阅读(327)  评论(0编辑  收藏  举报

导航