effective c++ 条款3 use const whereever you can
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的更多相关文章
- More Effective C++ 条款0,1
More Effective C++ 条款0,1 条款0 关于编译器 不同的编译器支持C++的特性能力不同.有些编译器不支持bool类型,此时可用 enum bool{false, true};枚举类 ...
- [More Effective C++]条款22有关返回值优化的验证结果
(这里的验证结果是针对返回值优化的,其实和条款22本身所说的,考虑以操作符复合形式(op=)取代其独身形式(op),关系不大.书生注) 在[More Effective C++]条款22的最后,在返回 ...
- Book. Effective C++ item2-尽量使用const, enum, inline替换#define
##常规变量 c++里面的#define后面的定义部分,是不算代码的一部分的.所以如果你使用#define: #define ASPECT_RATIO 1.653 你希望这个代号ASPECT RATI ...
- Effective C++ -----条款03:尽可能使用const
如果关键字const出现在星号左边,表示被指物是常量:如果出现在星号右边,表示指针自身是常量:如果出现在星号两边,表示被指物和指针两者都是常量. char greeting[] = " he ...
- Effective C++ -----条款02:尽量以const, enum, inline替换 #define
class GamePlayer{private: static const int NumTurns = 5; int scores[NumTurns]; ...}; 万一你的编译器(错误地)不允许 ...
- Effective C++ 条款03:尽可能使用const
场景一 用于修饰指针 char greeting[] = "Hello"; char* p = greeting; // non-const pointer, non-const ...
- Effective C++ 条款02:尽量以const,enum,inline替换 #define
换一种说法就是宁可以编译器替换预处理器 举例 #define ASPECT_RATIO 1.653 记号ASPECT_RATIO也许从未被编译器看见:也许在编译起开始处理源码前它就被预处理器移走了,于 ...
- Effective C++ 条款三 尽可能使用const
参考资料:http://blog.csdn.net/bizhu12/article/details/6672723 const的常用用法小结 1.用于定义常量变量,这样这个变量在后面就不可以 ...
- Effective C++ 条款08:别让异常逃离析构函数
1.别让异常逃离析构函数的原因 <Effective C++>第三版中条款08建议不要在析构函数中抛出异常,原因是C++异常机制不能同时处理两个或两个以上的异常.多个异常同时存在的情况下, ...
随机推荐
- CSU 1506(最小费用最大流)
传送门:Double Shortest Paths 题意:有两个人:给出路径之间第一个人走所需要的费用和第二个人走所需要的费用(在第一个人所需的 费用上再加上第二次的费用):求两个人一共所需要的最小费 ...
- iOS学习——JSON数据解析(十一)
在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...
- GitHub上最火的74个Android开源项目
GitHub上最火的74个Android开源项目 1.ActionBarSherlock ActionBarSherlock应该算得上是GitHub上最火的Android开源项目了,它是一个独立的库, ...
- VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表
原文:VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表 Excel具有强大的图表显示.分析功能,这点毋庸置疑,但是如果将常规MIS系统中的数据以报表的形式在Excel中显示,却并不那 ...
- poj3281(最大流)
传送门:Dining 题意:一些牛,一些食物,一些饮料,每头牛都有其喜欢的几种食物和几种饮料,求最多能给多少头牛即找到食物又找到饮料~也就是有多少个 牛---食物---饮料 的匹配,而且满足一一匹配, ...
- 在java代码中进行px与dip(dp)、px与sp单位值的转换
其实都是以前保存的代码,最近发现自己的资料库很混乱,索性都整理成博客,方便以后自己要用的时候快速找到. DisplayUtil.java /** * 单位转换工具 * * @author ca ...
- POJ-1324-Holedox Moving(BFS)
Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring ...
- 用JS实现发邮件的功能 完美解决
怎样用JS实现发邮件的功能? 我想用JS实现把页面文本框中的内容直接通过邮件的方式发送到一个指定的邮箱.fengxq给出的答案是<script language=javascript>if ...
- 一个linux常见命令的列表
这是一个linux常见命令的列表. 那些有• 标记的条目,你可以直接拷贝到终端上而不需要任何修改,因此你最好开一个终端边读边剪切&拷贝. 所有的命令已在Fedora和Ubuntu下做了测试 命 ...
- 共享库方案解决WAS中JAR包冲突
实现步骤: 1. 准备共享库JAR包 commons-httpclient-3.1.jar httpclient-4.3.3.jar httpcore-4.3.2.jar httpmim ...