关键字:
void short int long signed unsigned float double bool true false char wchar_t const
static_cast const_cast reinterpret_cast dynamic_cast sizeof typedef typeid
not not_eq and and_eq or or_eq xor xor_eq bitand bitor compl
if else switch case default do while for break continue goto return try catch throw
auto register new delete volatile static extern namespace using inline
class struct union enum asm
this public protected private explicit virtual mutable operator
template typename export
基础:
const全局变量没有外部链接性,const在typeid中不能被反映出来
每个文件都有独立的未命名名称空间,互不相干,可替代static取消外部连接性
全局对象初始化顺序不确定,使用Schwarz Counter (Nifty Counter)创建关键全局对象
POD (Plain Old Data)
ADL (Argument Dependent Lookup):函数匹配时,会同时在实参所在的名称空间中搜索
extern "C" 和throw(...) 是函数指针类型一部分
throw机制:异常变量复制(没有多态性),栈清理
栈清理过程中抛出异常,程序会调用terminate
抛出数组和函数,会转型成指针抛出
若new后构造函数抛出异常,只会调用相应的delete,所以new与delete要成对重载
类:
在类内定义成员函数和友元函数,都被隐式声明为inline
构造函数对成员变量的初始化顺序,由类中成员变量的定义顺序决定。
嵌套类可以访问外部类的静态成员、类型和枚举值,并且不受访问权限控制
派生类使用using,可以恢复基类成员权限,也可以恢复被隐藏的方法名称
在构造函数中调用虚函数,调用的是本类的虚函数(与Java不同)
基类:
纯虚函数可以给出定义,因此可将析构函数声明为纯虚函数以实现抽象类
复制构造函数需要被限制成explicit或者non-public,阻止对象剥离
析构函数需要被限制成virtual或者non-public,通过基类指针回收派生类对象
operator=,派生类不会自动调用基类的,加上virtual也不会成为虚函数
基类的new和delete会被派生类自动继承
EBO: Empty Base Class Optimization,空基类优化
范型:
部分特例化:对模板类成员部分特例化,对模板参数部分特例化
使用模板类型参数的内部类型时,需要加typename说明,否则认定为静态成员
模板类的派生类所用符号,不会在基类中搜索(基类有可能特例化),要用this->或using说明
隐式转型先于模板推导,若模板函数参数需要隐式转型,可声明成某类的友元非模板函数
若模板类的友元非模板函数依赖该类的模板类型参数,则其泛化版本只能在类内定义
模板类使用范型复制构造函数时,编译器仍有可能自动生成复制构造函数
标准库:
数学:
<complex>, <numeric>, <valarray>
算法、数据结构:
<algorithm>, <vector>, <deuqe>, <list>, <set>, <map>, <stack>, <queue>, <bitset>, <utility>, <iterator>, <functional>
STL保证基本异常安全,部分保证强异常安全
insert iterator (inserter), stream iterator, streambuf iterator, reverse iterator
字符处理:
<string>, <locale>
string使用copy-on-write技术
string的无效下标:string::npos
<locale> 中有一些字符分类函数
IO:
<ios>, <streambuf>, <istream>, <ostream>, <iostream>, <sstream>, <fstream>, <iomanip>
流标志:格式标志、状态、异常启发
流buf:streambuf, rdbuf()
流locale:imbue(), getloc()
流与cstdio同步,设置事件观察者,输入流tie输出流,通过manipulator影响流
国际化:
<locale>
locale对象是一些facet的集合,由use_facet<T>通过T做索引获得相应的facet
locale对象不可修改,要通过继承、构造、combine,将自定义facet置入locale对象
facet作用:
1) 定制格式:numpunct, moneypunct
2) 与流配合:numpunct, ctype, codecvt
3) 经streambuf迭代器IO,num_get, num_put, money_get, money_put, time_get, time_put
4) 辅助功能:ctype, collate(提供字符串hash)
其他:
<limits>, <memory>, <new>, <exception>, <stdexcept>
三个处理函数,set_new_handler, set_unexpected, set_terminate
三个特征类:numeric_limits<>, char_traits<>, iterator_traits<>
设计:
KISS: Keep It Simple Software
It is far, far easier to make a correct program fast than it is to make a fast program correct.
Don’t optimize or pessimize prematurely.
SRP: The Simple Responsibility Principle
OCP: The Open/Closed Principle
LSP: Liskov Substitution Principle
DIP: Dependency Inversion Principle
Classes should be easy to use correctly and hard to use incorrectly.
Don't expose internal information from an entity that provides an abstraction.
Avoid initialization dependencies across compilation units.
Pimpl (also Compilation Firewall or Cheshire Cat Technique): Private Implementation
Minimize definitional dependencies. Avoid cyclic dependencies.
NVI: No Virtual Interface
RAII: Resource Acquisition Is Initialization
Run-time Polymorphism, Compile-time Polymorphism
Local Synchronization Primitives, External Locking, Internal Locking, Lock Free
COW: Copy on Write (also Lazy Copy), considering std::string
Law of Second Chances: Interfaces must be right. Everything else can be fixed later.