#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[];
  *p = 'j'; 也不能,编译器信息:“return”: 无法从“const char”转换为“char &”
std::cout << *p;
}

成员函数的const不能被修改,包括指针的更多相关文章

  1. c/c++ 类成员变量,成员函数的存储方式,以及this指针在c++中的作用

    c/c++ 类成员变量,成员函数的存储方式,以及this指针在c++中的作用 c++不会像上图那样为每一个对象的成员变量和成员函数开辟内存空间, 而是像下图那样,只为每一个对象的成员变量开辟空间.成员 ...

  2. 成员函数的const到底修饰的是谁

    demo <pre name="code" class="cpp">class Test { public: const void OpVar(in ...

  3. 记得适当的声明成员函数为const.

    如果确信一个成员函数不用修改它的对象,就可以声明它为const,这样就可以作用于他的const对象了.因为const对象只能调用它的const方法. template<class T> c ...

  4. const成员函数和const对象

    从成员函数说起 在说const成员函数之前,先说一下普通成员函数,其实每个成员函数都有一个隐形的入参:T *const this. int getValue(T *const this) { retu ...

  5. 成员函数的const究竟修饰的是谁

    demo <pre name="code" class="cpp">class Test { public: const void OpVar(in ...

  6. mutable用于修改const成员函数中的成员变量

    http://no001.blog.51cto.com/1142339/389840/ mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中 ...

  7. c++中的const参数,const变量,const指针,const对象,以及const成员函数

    const 是constant 的缩写,“恒定不变”的意思.被const 修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性.所以很多C++程序设计书籍建议:“Use const whe ...

  8. 拷贝构造函数和const成员函数

    实验原因 说明如何使用const描述保护类数据不会意外修改. 编译环境 vc6sp6 + win7x64 工程下载 copyConstruction_constMemberFunction.zip   ...

  9. const关键字对C++成员函数的修饰

    const对C++成员函数的修饰分为三种:1. 修饰参数:2. 修饰返回值:3. 修饰this指针.简述一下知识点如下,以后找功夫再完善. 1. 对函数参数的修饰. 1)const只能用来修饰输入参数 ...

随机推荐

  1. node学习笔记-搭建node环境

    最近项目要用到node,利用空闲整理做下笔记 第一步  安装node,方式比较多,最为直接的是直接去官网     可直接从官网下载安装http://nodejs.cn/download/ 根据自己情况 ...

  2. angular启动过程分析

    启动过程 步骤一 用自执行函数在代码完成加载后立即执行 function(window, document, undefined) 在window上暴露一个唯一的全局对象angular,Line250 ...

  3. Redis+PHP扩展的安装和Redis集群的配置 与 PHP负载均衡开发方案

    以前有想过用 Memcache 实现M/S架构的负载均衡方案,直到听说了 Redis 后才发现它做得更好.发了几天时间研究了一下 Redis ,感觉真的很不错,特整理一下! 以下操作都是在 SUSE  ...

  4. 最完美的匹配网页中图片 src 部分的正则表达式

    $str='<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"& ...

  5. Java Tomcat 中调用.net DLL的方法

    近日一个java的项目,客户要求项目中必须使用其提供的加密机制,扔给了两个.net写的DLL.网络上搜了一圈也没找到啥东西,甚至看到人扬言此事绝无可能.郁闷当中考虑了一个思路.用C#做一个Com,调用 ...

  6. python_基本语法_01

    离毕业工作还有几个月,本来想早点去公司实习,无奈gb学校不给放.好吧,既然这样,就学门语言. 参考与 http://www.cnblogs.com/vamei ,我是跟着这位博客牛人的博客教程学的,具 ...

  7. PreparedStatement 和 Statment区别

    PreparedStatement vs Statment 1)语法不同:PreparedStatement可以使用预编译的sql,而Statment只能使用静态的sql 2)效率不同: Prepar ...

  8. How systems researchers build systems

    Define the problem >>Identify the constraints and abstract problem propose solution:simple ide ...

  9. Big Data Analytics for Security(Big Data Analytics for Security Intelligence)

    http://www.infoq.com/articles/bigdata-analytics-for-security This article first appeared in the IEEE ...

  10. 在SQL Server中使用命令调用SSIS包

    在SQL Server中可以使用dtexec命令运行SSIS包(2005以上版本),当然也可以通过系统过程:xp_cmdshell调用dtexec运行SSIS包. 具体操作步骤如下: 1.首先,当然是 ...