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. CSDN编程挑战——《高斯公式》

    高斯公式 题目详情: 高斯在上小学时发明了等差数列求和公式:1+2+..+100=5050.如今问题在于给你一个正整数n,问你他能够表示为多少种连续正整数之和?(自身也算). 输入格式: 多组数据,每 ...

  2. 在navigationItem中添加搜索栏

    给navigationItem中添加个搜索栏,效果和大部分程序一样.代码如下: UISearchBar *searchBar = [[UISearchBaralloc] initWithFrame:C ...

  3. 高效合并两个有序数组(Merge Sorted Array)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  4. ffplay2 android 版正式公布

    项目地址:https://github.com/DeYangLiu/AndroidPlayer/ 下载链接: 看点: 支持软键盘输入和历史记录.使用了EditText和内部存储. 这里考虑了历史记录的 ...

  5. Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02

    /* 场景一lua代码 */ require "MainScene02" local dic_size = CCDirector:sharedDirector():getWinSi ...

  6. PhantomJS是一个基于WebKit的服务器端JavaScript API

    PhantomJS是一个基于WebKit的服务器端JavaScript API,它基于 BSD开源协议发布.PhantomJS无需浏览器的支持即可实现对Web的支持,且原生支持各种Web标准,如DOM ...

  7. 【iOS开发】 常遇到的Crash和Bug处理

    一,Unknown type name .... 如果是报这个错误,多半是你的对象类型没有被识别,检查是不是没有引用对应的库或者头文件在你的文件头部分,还有可能是循环引用导致的,循环引用的解决方法就是 ...

  8. Codeforces 164 E Compatible Numbers

    主题链接~~> 做题情绪:好题,做拉的比赛的时候想了非常久,想到枚举变幻某一位的 0 为 1 .可是每一个数都这样枚举岂不超时的节奏,当时没想到事实上从大到小枚举一次就 ok 了. 解题思路: ...

  9. NOJ1184 失落的邮票 哈希表

    意甲冠军 我们共收集N邮票.现在失去了2张,剩下N-2张-..原集邮收集了所有对.因此,找到什么两枚邮票是一个.它们输出. (确定缺少邮票是不一样的) 思路 由于编号比較大,能够用hash表压缩成数组 ...

  10. Windows编程之非模态对话框

    1  创建非模态对话框 <1>  HWNDCreateDialog(  HINSTANCE hInstance,  // handle to module LPCTSTRlpTemplat ...