将某些东西声明为const可以帮助编译器检测出错误用法,const

编译器强制实施bitwise constness,但是你code的时候应该使用“概念上的常量性”

当const和non-const成员函数有着实质等价的实现时,令non-const版本调用const版本可以避免代码重复。

0)迭代器类似T*指针,声明迭代器为const就像声明指针为const一样。(即声明一个T* const指针)

如果你希望迭代器所指的不能被改动,需要的是const_iterator

vector<int>::iterator iter = vec.begin();//iter作用像T* const

*iter  = 10;//没问题,改变iter所指物

iter++;//错误,iter是const

vector<int>::const_iterator cIter = vec.begin();//作用像const T*

*cIter  = 10;//错误 *cIter是const

iter++;//OK

返回值为const 防止(a*b)=c;这样的暴行

1)const成员函数之所以重要,因为a)知道哪个函数可以改动对象内容;b)使得“操作const对象”成为可能。

两个成员函数如果只是常量性(constness)不同,可以被重载。

#include <iostream>
#include <string.h>
using namespace std;

class TextBlock
{
private:
    char text[256];
    
public:
    TextBlock(char *input)
    {
        strcpy(text,input);
    
    }
    //!!!REMEMBER IT!!!!
    //operator[] for const object
    const char& operator[](std::size_t position) const
    {
        cout << "now in the const member\n";
        return text[position];
    }
    //operator[] for non-const object
    char& operator[](std::size_t position)
    {
        cout << "now in the non-const member\n";
        return text[position];
    }            
};

int main()
{
    TextBlock tb("hello");
    cout << tb[0] <<'\n';
    const TextBlock ctb("world");
    cout << ctb[0] <<'\n';
    
    return 0;
}

now in the non-const member
h
now in the const member
w

2)在const和non-const成员函数中避免重复

class TextBlock

{

public:

  const char& operator[](size_t position) const

  {  //bounds checking,log access data,verify data integrity...

    return text[position];

  }

  char& operator[](size_t position)//现在只调用const op[]

  {

    return const_cast<char&>(//将op[]返回值的const转除

      static_cast<const TextBlock&>(*this)[position]);//为*this加上const调用const op[]

      

  }//代码写的真巧妙,我这辈子估计都写不出这么巧妙的代码了。。。。

  

};

转载于:https://www.cnblogs.com/jeanschen/p/3178443.html

[EffectiveC++]item3:尽可能使用const相关推荐

  1. C++尽可能使用const

    尽可能使用const const修饰变量 const修饰函数 const修饰成员函数 const修饰变量 如果变量本身不应该被修改,应该使用const修饰.这样编译器可以进行保护,确保这个变量不会被修 ...

  2. char* 赋值 const char* 释放_Effective C++读书笔记之条款3:尽可能使用const

    点击上方蓝字关注我们 C++的const关键字会强制编译器实施保持某个值不变的约束,帮助你在编译期间就能发现错误,灵活使用const能提高代码质量还能避免不想被改变的值被修改.下面我将从const作用 ...

  3. 【Effective C++ 条款03 笔记】尽可能使用const

    条款03:尽可能使用const 一.顶层const和底层const 区分一下: int a = 1; const int* pa = a;//底层const,a的值不能变 int b = 2; int ...

  4. 条款 03:尽可能使用const

    条款 03:尽可能使用const 1.请记住 2.原因 3.const用法 3.1.在classes外部修饰global或namespace作用域中的常量 3.2.修饰文件.函数.或区块作用域中被声明 ...

  5. 【转】C++ const用法 尽可能使用const

    http://www.cnblogs.com/xudong-bupt/p/3509567.html C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不 ...

  6. effective C++ 条款 3:尽可能使用const

    const 修饰指针:如果关键字const出现在星号*左边,表示被指物是常量,如果const出现在*右边表示指针自身是常量, 如果出现在两边表示被指物和指针都是常量: const std::vecto ...

  7. Effective C++ 条款03:尽可能使用const

    场景一 用于修饰指针 char greeting[] = "Hello"; char* p = greeting; // non-const pointer, non-const ...

  8. const 常量_条款03:尽可能使用const

    const 允许你指定一个语义约束(也就是指定一个"不该被改动"的对象),而编译器会强制实施这项约束. 1.const指针 如果关键字const出现在星号左边,表示被指物是常量:如 ...

  9. C++ const用法 尽可能使用const

    C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就应该明确使用const,这样可以获得编译器的帮助. 1.c ...

  10. 【03】尽可能使用const

    1.为什么搞出const关键字? const指定一个语义约束,指定一个对象不可修改.如果一个对象不可修改,就应该说出来. 2.const与指针 const可以修饰指向之物,也可以修改指针本身.STL中 ...

最新文章

  1. 用最少的时间学最多的数据挖掘知识(附教程数据源)| CSDN博文精选
  2. redis 持久化之 rdb 快照持久化
  3. lambda中的钩子函数
  4. boost::proto::switch_相关的测试程序
  5. awesome字体图标库
  6. 如何使用Kotlin构建具有在线状态的Android Messenger应用
  7. mysql孤立锁_SQL Server解决孤立用户浅析
  8. OpenShift 4 之运行Istio的BookInfo微服务应用
  9. SpringMvc JavaMailSenderImpl 邮件发送时到时前端无法接受Json数据问题解决
  10. oir 用image j打开的插件_Windows 上使用 VSCode Remote 插件进行远程开发
  11. python中numpy matplotlib绘图教程_利用numpy+matplotlib绘图的基本操作教程
  12. layer 父弹出框上弹出子弹框窗体大小问题
  13. 利用predis操作redis方法大全
  14. stixel_world+Multi_stioxel_world+semantic_stixel_world知识拓展
  15. 的正确使用_如何正确使用隔离霜
  16. 整合hibernate4到spring4mvc框架
  17. 计算机一级cad试题,全国计算机等级考试一级试题与答案(25套)(1)2
  18. 骇客基础_骇客基础知识:第3部分
  19. 软著申请模板,帮助了不少小伙伴少走弯路
  20. 静夜思 | 你的眼界,决定了你发现美好的能力

热门文章

  1. Android开发之来电电话挂断实现
  2. Android制作自己的依赖库让别人引用【转】
  3. 开博客了,大家好,这是ATHENS的博客。
  4. 前端移动App开发环境搭建
  5. Nginx+tomcat整合
  6. 从Android support到Androidx
  7. [Material Design] MaterialButton 效果进阶 动画自动移动进行对齐效果
  8. 仿Android 5.0 侧滑菜单按钮动画 以及侧滑菜单联动
  9. CF991E Bus Number
  10. 1006 A+B问题