1 const 传达的意思应该是这个变量是常量不能更改

2 const 在 * 左边表示数据是const,在右边表示指针是const

   //
char greeting[] = "hello"; char* p = greeting; //const *: const data
//* const: const pointer
char const *p1 = greeting; // const data
char * const p2 = greeting; // const pointer
const char * const p3 = greeting; // const data and pointer //not correct p1 is const data
//(*p1)++; //correct p1 is const data
p1++; //correct p2 is const pointer
(*p2)++; //not correct p2 is const pointer
//p2++; //not correct p3 is const data and pointer
//(*p3)++; //not correct p3 is const data and pointer
//p3++;

  3

//1 const return value avoid the error like this
// if( a+b = c) {...}
//2 const parameter
// it can avoid unintentioned change of parameter inside the function
const Rational operator+ (const Rational& lhs, const Rational& rhs)
{
Rational tmp;
tmp.real = lhs.real + rhs.real;
tmp.img = lhs.img + rhs.img; return tmp;
} class Rational
{
public:
float real;
float img; Rational():real(0),img(0){}; };

  4 const 成员函数

声明方式

作用

ConstMemberFunc.h

//---------------------------------------------------------------------------

#ifndef ConstMemberFuncH
#define ConstMemberFuncH
//--------------------------------------------------------------------------- #include <cstddef> //for std::size_t //1 const member function is used to operate const object
// eg. we have a class Textblock and it has a operator[] which is const
// when we have a const Textblock, the operator[] const will be used //2
// a const member function can change static class member
// b const member function can not change non-static class member
// c use mutable decleared variable can be changed in const member function //3 const member function can only call const member function
// can not call non-const member function //4 const object can only call const member function
// const object can not call non const member function
class TextBlock
{
public: static int TextBlockStaticNum; char* text; //1 const member function is used to operate const object
// eg. we have a class Textblock and it has a operator[] which is const
// when we have a const Textblock, the operator[] const will be used
const char& operator[](std::size_t position) const
{
return text[position]; //validate 3
//not correct
//3 const member function can only call const function
// can not call non-const function
// print(); is the non-const function of vector }; char& operator[](std::size_t position)
{
//return text[position]; //call const operator[] and remove constness for return
//not recommended
return const_cast<char&>( // remove the const from the return of const []
static_cast<const TextBlock&>(*this) // add const to *this
[position] // call const []
);
}; //2
// a const member function can change static class member
// b const member function can not change non-static class member
// c use mutable decleared variable can be changed in const member function
int nonStaticOrMutable;
mutable int textLen;
const void constMemberFunctionTest(int Num) const
{
//a change static member
TextBlockStaticNum = 1; //b
//not correct
//can not change non-static class member
//nonStaticOrMutable = 1; //c use mutable decleared variable can be changed in const member function
textLen = 2; }; //3 const member function can only call const member function
// can not call non-const member function
void print()
{
;
} TextBlock( char charArray[])
{
text = charArray;
TextBlockStaticNum = 0;
textLen = 0;
nonStaticOrMutable=0;
};
} ; //1 const member function is used to operate const object
// eg. we have a class Textblock and it has a operator[] which is const
// when we have a const Textblock, the operator[] const will be used
//Test function
extern void ConstAndNonConstObjCallConstMemberFunction(); //2
// a const member function can change static class member
// b const member function can not change non-static class member
// c use mutable decleared variable can be changed in const member function
extern void ConstMemFunctionChangeClassMember(); //4 const object can only call const member function
// const object can not call non const member function
extern void ConstObjCanNotCallNonConstMember(const TextBlock & ctb)
{
//not correct
//print is not a const member function
//ctb.print(); //correct
const char t = ctb[0]; }; #endif

 ConstMemberFunc.cpp 

//---------------------------------------------------------------------------

#pragma hdrstop

#include "ConstMemberFunc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init) #include "ConstMemberFunc.h" void ConstAndNonConstObjCallConstMemberFunction()
{
TextBlock tb("test");
const TextBlock ctb("test"); //call non const operator[]
//correct
tb[0] = '1'; //call const operator[]
//not correct, since ctb is const operator
//ctb[0] = '1';
//normally this is used as a function call
//e.g. void print(const TextBlock ctb) //we do not change ctb in this function
} void ConstMemFunctionChangeClassMember()
{
TextBlock tb("test");
tb.constMemberFunctionTest(5); const TextBlock ctb("test");
ctb.constMemberFunctionTest(2);
}

  

effective c++ 条款3 use const whereever you can的更多相关文章

  1. More Effective C++ 条款0,1

    More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...

  2. [More Effective C++]条款22有关返回值优化的验证结果

    (这里的验证结果是针对返回值优化的,其实和条款22本身所说的,考虑以操作符复合形式(op=)取代其独身形式(op),关系不大.书生注) 在[More Effective C++]条款22的最后,在返回 ...

  3. Book. Effective C++ item2-尽量使用const, enum, inline替换#define

    ##常规变量 c++里面的#define后面的定义部分,是不算代码的一部分的.所以如果你使用#define: #define ASPECT_RATIO 1.653 你希望这个代号ASPECT RATI ...

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

    如果关键字const出现在星号左边,表示被指物是常量:如果出现在星号右边,表示指针自身是常量:如果出现在星号两边,表示被指物和指针两者都是常量. char greeting[] = " he ...

  5. Effective C++ -----条款02:尽量以const, enum, inline替换 #define

    class GamePlayer{private: static const int NumTurns = 5; int scores[NumTurns]; ...}; 万一你的编译器(错误地)不允许 ...

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

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

  7. Effective C++ 条款02:尽量以const,enum,inline替换 #define

    换一种说法就是宁可以编译器替换预处理器 举例 #define ASPECT_RATIO 1.653 记号ASPECT_RATIO也许从未被编译器看见:也许在编译起开始处理源码前它就被预处理器移走了,于 ...

  8. Effective C++ 条款三 尽可能使用const

    参考资料:http://blog.csdn.net/bizhu12/article/details/6672723      const的常用用法小结 1.用于定义常量变量,这样这个变量在后面就不可以 ...

  9. Effective C++ 条款08:别让异常逃离析构函数

    1.别让异常逃离析构函数的原因 <Effective C++>第三版中条款08建议不要在析构函数中抛出异常,原因是C++异常机制不能同时处理两个或两个以上的异常.多个异常同时存在的情况下, ...

随机推荐

  1. OCA读书笔记(9) - 管理数据同步

    9.Managing Data Concurrency 描述锁机制以及oracle如何管理数据一致性监控和解决锁冲突 管理数据的并发--管理锁数据的不一致:脏读更改丢失幻影读 脏读:数据是指事务T2修 ...

  2. MVVM Light须要注意的10个问题

    MVVM Light须要注意的10个问题 从使用XAML技术基础開始(实际上并非非常久曾经).我便关注MVVM(Model – View – ViewModel)模式.偶然接触到MVVM Light不 ...

  3. Android NDK入门实例 计算斐波那契数列二生成.so库文件

    上一篇文章输生成了jni头文件,里面包含了本地C代码的信息,提供我们引用的C头文件.下面实现本地代码,再用ndk-build编译生成.so库文件.由于编译时要用到make和gcc,这里很多人是通过安装 ...

  4. C++操作符operator的另一种用法

    http://blog.csdn.net/memewry/article/details/7833314 参考地址 今天在程序员面试宝典上看到这样一道题目:  A C++ developer want ...

  5. poj2479(dp)

    题目链接:http://poj.org/problem?id=2479 题意:求所给数列中元素值和最大的两段子数列之和. 分析:从左往右扫一遍,b[i]表示前i个数的最大子数列之和. 从右往左扫一遍, ...

  6. PHP上传文件超过了最大文件大小限制导致无法上传成功

    最近的研究<HeadFirst PHP & MySQL>第一本书5章"使用存储在文件中的数据",难道当一个文件上传应用程序,发生了错误.即,文件不能成功上传.这 ...

  7. 《火球——UML大战需求分析》(第1章 大话UML)——1.3 行为型的UML(Behavior Diagram)

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  8. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  9. linux--关于shell的介绍

    下面是最近学习shell的一些知识点总结***博客园-邦邦酱好*** 1.什么是shell(1)Shell将我们输入的指令与Kernel沟通,好让Kernel可以控刢硬件来正确无误地工作.(2)我们总 ...

  10. U9文件与文件系统的压缩和打包

    1.在Linux的环境中,压缩文件的扩展名大多为:*.tar,*.tar.gz,*.tgz,*.bz2. 2.gzip可以说是应用最广的压缩命令了.目前gzip可以揭开compress,zip和gzi ...