首页 > 其他 > 详细

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

时间:2016-06-11 11:44:02      阅读:121      评论:0      收藏:0      [点我收藏+]
#include <iostream>

class A
{
private:
    std::string a;
public:
    A(std::string b) :a(b){}
    char& operator[](int b)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; }

 

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

原文:http://www.cnblogs.com/vhyc/p/5574821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!