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. 11.4.2 排序或合并文件(sort命令) - 51CTO.COM

    11.4.2 排序或合并文件(sort命令) - 51CTO.COM 11.4.2 排序或合并文件(sort命令) 2010-03-12 14:37 陆松年 电子工业出版社 我要评论(0) 字号:T ...

  2. C++环形矩阵填充实现

    #include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #incl ...

  3. 彻底删除&quot;提示删除文件和目录&quot;时出错的文件或目录

    当删除文件是出现上图时  能够用以下的方法删除文件 策略一:系统大法 第一招  进程帮你搞定 很多时候乱码文件名称里的文件是explorer.exe进程联系在一起的. 假设要删除的话能够这样:首先命令 ...

  4. No-Touch Integration 在SharePoint中使用社区支持的Silverlight应用程序

    No-Touch Integration 在SharePoint中使用社区支持的Silverlight应用程序         No-Touch Integration应该是最简单的方法了.将Silv ...

  5. SPOJ DISUBSTR(字符串hash)

    传送门:DISUBSTR 题意:给定一个字符串,求不同子串个数. 分析:由于数据较小,直接枚举长度为1,2...n的所有子串进行hash即可,复杂度(O(n^2)),后缀数组才是正解(O(nlogn) ...

  6. 拿到阿里,网易游戏,腾讯,smartx的offer的过程 (转)

    前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...

  7. Net Kafka

    Net Kafka Kafka 协议实现中的内存优化 Jusfr 2016-04-18 08:28 阅读:241 评论:1     Kafka API: TopicMetadata Jusfr 201 ...

  8. 10招让你成为杰出的Java程序员(转)

    如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...

  9. js封装好的模仿qq消息弹窗代码

    在我们的日常开发中,或者生活中.常常须要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  10. android圆角View实现及不同版本号这间的兼容

    在做我们自己的APP的时候.为了让APP看起来更加的好看,我们就须要将我们的自己的View做成圆角的,毕竟主流也是将非常多东西做成圆角.和苹果的外观看起来差点儿相同,看起来也还不错. 要将一个View ...