c with classes
尽量以const enum inline 替换#define
示例代码:
#define ASPECT_RATIO 1.653 //不进入记号表(symbol table)
替换为
const AspectRatio 1.653 //进入记号表
定义常量字符串
const std::string authorName("retacn");
class专属常量
示例代码如下:
class GamePlayer{
private:
static const int NumTurns=5;
int scores[NumTurns];
...
};
the enum hack替换define
class GamePlayer{
enum{NumTurns=5};
int scores[NumTurns];
}
注:取一个const的地址是合法的,取一个enum的地址是不合法有
如果不想让别人获得一个pointer或reference指向你的某个整数常量,可以使用其约束
template inline替换宏定义(macros)
示例代码如下:
#define CALL_WITH_MAX(a,b) f((a)>(b)?(a):(b))
替换为:
template<typename T>
inline void callWithMax(const T& a,const T& b){
f(a>b?a:b);
}
#include #ifdef #ifndef 仍是控制编译的重要角色
const的使用
const修饰指针
示例代码如下:
char greeting[] ="hello world"
char* p =greeting; //non-const pointer,non-const data
const char* p=greeting; //non-const pointer,const data
char* const p =greeting; //const pointer,non-const data
const char* p const p =greeting; //const pointer,const data
注:如果出现在*的左边,表示物是常量
分为以下两种情况,可以位于类型的左边或是右边:
void f1(const Widget* pw);
void f2(Widget const * pw);
如果出现在*右边,表示指针自身是常量
如果两边都出现,表示两者都是常量
const 修饰迭代器
示例代码如下:
std::vector<int> vec;
...
const std::vector<int>::iterator iter=vec.begin();//迭代器不得指向不同的东西
*iter=10; //可以
++iter; //错误,iter是const
std:vector<int>::const_iterator cIter=vec.begin();//迭代器所指的东西不可变
*cIter=10; //错误 *cIter是const
++cIter; //可以
const修饰函数
//令函数返回一个常量数
class Rational{...};
const Rational operator* (const Rational& lhs,const Rattional& rhs);
const修饰成员函数
//使成员函数可以作用于一个const对象身上
示例代码如下;
class TestBlock{
public:
..
const char& operator[](std::size_t position) const{
return text[position]; //operator[]for const 对象
}
char& operator[](std::size_t position){
return text[position];//operator[]for non-const对象
}
}
//使用
TextBlock tb("hello");
std::cout<< tb[0];//调用non-const TextBlock::operator[]
const TextBlock ctb("world");
std::cout<<ctb[0]; //调用const TextBlock::operator[]
std::cout<<tb[0];//可以
tb[0]='x';//可以
std::cout<<ctb[0];//可以
ctb[0]='x';//错误,不能写一个const TextBlock
bitwise const
示例代码如下:
class CTextBlock{
public:
...
char& operator[](std::size_t position)const{
return pText[position];
}
private:
char* pText;
}
//使用
const CTextBlock cctb("hello");//声明一个常量对象
char* pc=&cctb[0]; //指向cctb的数据
*pc='j'; //cctb的内容修改为"jello"
logical constness
示例代码如下:
class CTextBlock{
public:
...
std::size_t length() const;
private:
char* pText;
mutable std::size_t textLength; //可以被修改,即使在成员函数内
mutable bool lengthIsValid;//同上
}
std:size_t CTextBlock::length() const{
if(!lengthIsValid){
textLength=std::strlen(pText);
lengthIsValid=true;
}
return textLength;
}
当用const和non-const成员函数有着实质等价的实现时,令non-const版本调用const版本可避免代码重复
示例代码如下:
class TextBlock{
public:
...
const char& operator[](std::size_t position)const{
...
return text[position];
}
char& operator[](){
//将op[]返回值的const转除为*this加上const调用const op[]
return const_cast<char&>(static_cast<const TextBlock>(*this)[position]);
}
};
对象使用前需要先行初始化
示例代码如下:
int x=0; //基本类型手工初始化
const char* text="A C-style string";//指针手工初始化
double d;
std::cin>>d; //以读取input stream 的方式进行初始化
构造函数将对象进行初始化
示例代码如下;
class PhoneNumber{...};
//address book entry
class ABEntry{
public:
ABEntry(const std::string& name,const std::string& address,const std::list<PhoneNumber>& phone);
private:
std::string theName;
std::string theAddress;
std::list<PhoneNumber> thePhones;
int numTimesConsulted;
};
//构造函数(成员初始列 member initialization list)对其对象进行初始化
ABEntry::ABEntry(const std::string& name,const std::string& address,const std::list<PhoneNumber>& phones)
:theName(name),theAddress(address),thePhone(phones),numTimeConsulted(0)
{}
//对其对象进行赋值
ABEntry::ABEntry(const std::string& name,const std::string& address,const std::list<PhoneNumber>& phones){
theName=name;
theAddress=address;
thePhones=phones;
numTimesConsulted=0;
}
不同编译单元内定义之non-local static对象的初始化
两个源码文件,每个文件内至少包含一个non-local static对象
static对象的生命周期,从构造出来直到程序结束为止.
函数内的static对象称为local static
其他(global对象、定义于namespace作用域内的对象、在class内、在file作用域内)对象称为non-local static
编译单元(translation unit) 指产出单一目标文件(single object file)的源码
示例代码如下:
//应用场景
//单一文件系统
class FileSystem{
public:
...
std::size_t numDisks() const; //成员函数
...
};
extern FileSystem tfs;//预备给客户使用
//客户用以处理文件系统内的目录
class Directory{
public:
Directory(params);
...
};
Directory::Directory(params){
...
std::size_t disks=tfs.numDisks();//使用文件系统对象
...
}
//创建一个Directory对象,用来处理临时文件
Directory tempDir(params);
//好的处理方式 看过design patterns的话,这就是单例模式的一个实现手法
//以函数调用(返回一个reference指向local static对象)替换"直接访问non-local static对象"
class FileSystem{...};
FileSystem& tfs(){
static FileSystem fs;
return fs;
}
class Directory{...};
Directory::Directory(params){
...
std::size_t disks=tfs().numDisks();
}
Directory& tempDir(){
static Directory td;
retrun td;
}
- 浏览: 250215 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (303)
- c (31)
- c++ (16)
- java (18)
- c# (1)
- python (3)
- java web (6)
- oracle (7)
- sqlserver (2)
- mysql (2)
- android (24)
- android系统 (15)
- android多媒体部分 (15)
- android游戏 (12)
- linux (26)
- javaScript (1)
- ajax (1)
- node JS (2)
- html (5)
- apache (3)
- jboss (1)
- weblogic (0)
- 通信协议 (10)
- 云计算 (1)
- 分布式 (5)
- ejb (1)
- webservice (5)
- 设计模式 (16)
- JNI (6)
- swing (13)
- 版本控制 (1)
- UML (1)
- xml (4)
- spring (5)
- hibernate (5)
- struts1 (3)
- struts2 (4)
- ibatis (0)
- tomcat (2)
- 心得体会 (1)
- css (1)
- 嵌入式 (41)
- arm体系结构 (10)
发表评论
-
c++学习笔记十七
2013-05-18 23:44 849构造、析构、赋值运算 c++会为一个空类声明一个copy构造函 ... -
c++学习笔记一
2012-09-03 15:25 583基本概念 第一个程序 h ... -
c++学习笔记二
2012-09-03 15:26 332处理基本数据类型 1 混合表达式 (向上转型) 2 赋 ... -
c++学习笔记三
2012-09-03 15:26 520选择和决策 比较数据值 1 关系运算符:< &g ... -
c++学习笔记五
2012-09-03 15:27 576while do-while for循环 循环和变 ... -
c++学习笔记六
2012-09-03 15:28 238指针 可以使用指针记 ... -
c++学习笔记七
2012-09-03 15:28 536使用函数编程 程序的分解 1 函数 定义函 ... -
c++学习笔记八
2012-09-03 15:29 577函数 1 函数的重载: 函数名相同,参数个数不同 ... -
c++学习笔记九
2012-09-20 23:14 674程序文件和预处理指令 1 程序文件 头文件:类型定 ... -
c++学习笔记十
2012-09-20 23:18 573创建自已的数据类型 1 对像的概念 2 c++中的结构 ... -
c++学习笔记十一
2012-09-20 23:20 220类 1 封装 继承 多 ... -
c++学习笔记十二
2012-09-20 23:20 666类的操作 1 类对象使用指针的三种情况: ... -
c++学习笔记十三
2012-09-20 23:31 630运算符重载 1 为自已的类实现运算符 运 ... -
c++学习笔记十四
2012-09-20 23:32 569继承 1 类和面向对象编程 2 类的继承 继承 ... -
c++学习笔记十五
2012-09-20 23:36 604虚函数和多态性 使用基类指针,示例代码如下(从Box派 ...
相关推荐
c++学习笔记/知识要点精华版
C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记
c++学习笔记.pdf
C++学习笔记C++学习笔记C++学习笔记C++学习笔记C++学习笔记
C++学习笔记
【C++学习笔记】一份详细的学习笔记,让你轻松掌握C++编程!
C++学习笔记.docx
C++学习笔记: 以学习、思考、记录、分享为乐。
c++完美学习笔记c++完美学习笔记c++完美学习笔记c++完美学习笔记
C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记002
C++学习笔记
C++基础的学习笔记(其中的代码源件可私信留言) C++中的空头程序 使用C++书写hello world 学习变量 学习常量 标识符命名规则 数据类型 运算符 程序流程结构 数组 函数 指针 结构体
C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记003
C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记004
C++ 学习笔记C++ 学习笔记C++ 学习笔记C++ 学习笔记001
C++学习笔记经典(与C比较),是学习C++的好资料。
Visual C++学习笔记,切身学习体会和总结
适用人群:C++初学者及C++期末考试冲刺 此C++学习笔记是本人在学习中思考总结所得,能够很好地帮助你入门C++或冲刺期末考试,让你掌握C++基础。
该笔记是我在mooc上看C++程序设计时做的一些笔记,因为是突发学习C++,要什么就学习什么,现在已经学到“文件输入输出流”,因为现在的项目不需要用到模板的知识,所以暂时只学到这么多,之后肯定还得学的,这个老师...