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++异常机制不能同时处理两个或两个以上的异常.多个异常同时存在的情况下, ...
随机推荐
- Java中double变量精确到小数点后几(2)位
import java.math.BigDecimal; import java.text.NumberFormat; public class Java中double类型的数据精确到小数点后两位 { ...
- OCaml Language Sucks
OCaml Language Sucks OCaml Language Sucks
- 用TinyXml2读取XML文件的一个简单Demo
废话少说直接上代码,需要的人自然一看便懂,对于第一次接触TinyXml2的人来说还是有帮助的. <?xml version="1.0"?> <Table name ...
- cocos2d 走动椭圆
1.效果图 艺术与规划说他想与我合作在全国率先主角光环加,椭圆形走动. cocos2d自带没有,參考网上的写了一个. 2.椭圆数学知识 有关椭圆的数学知识我已经忘光了.网上找了点资料: a是椭圆的长半 ...
- hadoop每个家庭成员
本文没有提到的原则.谈论hadoop项目周边,它的作用. hadoop这个词已经流行了很多年.大数据的记载会认为hadoop,然后hadoop的作用是什么呢? 官方定义:hadoop是一个开发和执行处 ...
- iOS发展 ---- 至iPhone 6自适应布局设计 Auto Layout
Apple从iOS 6增加了Auto Layout后開始就比較委婉的開始鼓舞.建议开发人员使用自适应布局,可是到眼下为止,我感觉大多数开发人员一直在回避这个问题,无论是不是因为历史原因造成的,至少他们 ...
- 俄罗斯方块游戏JavaScript代码
JavaScript代码俄罗斯方块游戏 早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用 ...
- SpringMVC与Mybatis框架整合遇到的坑(转)
最近在做springmvc与mybatis的项目,遇到一些比较坑的问题.花了许多时间却发现其实解决的办法很简单.这里主要是讲我自己在整合这两个框架的时候遇到的一些问题做一个整理.希望遇到和我同样问题的 ...
- Python眼睛护士改进版
添加了设定从(0,0)显示:self.root.geometry('1000x200+0+0')其实主要是两个0.那个1000和200是没用的,因为已经设定了minsize. 添加了窗口置顶:self ...
- Date和String类型的相互转换
String转Date: SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); String ...