不得不说,Effective C++确实是一本C++进阶的好书,刚浏览完第二遍,
现对其做一个大体性的总结 ,并进行适当的展开,作为以后C++参考复习之用。
(一).让自己习惯C++
条款1:视C++为一个语言联邦
#1.将C++ 分为四个次语言:C,Templete C++, Object-Oritented C++, STL;____________________________________________________________________________________________________________________________________
理由3:#defines不能用来定义class专属常量,而const可以。
#2.用enum替代#define而const可以取地址,这可以避免pointer和reference指向你enum中的某个常量,实现约束。
#3.用inline替代#define换用inline function可享有这种宏带来的效率并且能正确执行。
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
条款4:确定对象被使用前已先被初始化理由2:local static 对象会在函数调用期间首次遇上时初始化,因此未使用时没有初始化成本。
____________________________________________________________________________________________________________________________________
(二).构造/析构/赋值运算
____________________________________________________________________________________________________________________________________
derived class 生成一个copy assignment操作符。
____________________________________________________________________________________________________________________________________
class HomeForSale{ public: ... private: HomeForSale(const HomeForSale&); HomeForSale operator=(const HomeForSale&); }private意味着企图拷贝HomeForSale对象时,会被编译器拒绝,若不慎
class MyHomeForSale:private HomeForSale{ ... };此时尝试生成copy构造函数和copy assignment操作符时,会因调用base class对应
函数版本失败而被编译器拒绝。
____________________________________________________________________________________________________________________________________
#2.class中有virtual函数意味着带多态性质,因此也该拥有一个virtual析构函数。
____________________________________________________________________________________________________________________________________
class DBConn{ public: ... void close() { db.close(); bClosed = true; } ~DBConn { if(!bClosed) { try{ db.close(); bClosed = true; }catch(...) { //制作运转记录,记下对close的调用失败 ... } } } private: DBConnection db; bool bClosed; }
____________________________________________________________________________________________________________________________________
条款9:绝不在构造和析构函数中调用virtual函数class Transaction { public: explicit Transaction(const std:string& logInfo); void logTransaction(const std:string&logInfo) const; ... }; Transaction::Transaction(const std::string& logInfo) { ... logTransaction(logInfo); } class BuyTransaction:public Transaction{ public: BuyTransaction(parameters); :Transaction(createLogString(parameters)){...} ... private: static std::string createLogString(parameters); };
____________________________________________________________________________________________________________________________________
条款10:令opertator=(+=,-=,*=类似运算操作符)返回一个reference to *thistr1:shared_ptr等所遵守,为了统一起见,还是遵守吧!
____________________________________________________________________________________________________________________________________
原文:http://blog.csdn.net/beyond_ray/article/details/43636165