Effective C++笔记(一)——条款26-29
生活随笔
收集整理的這篇文章主要介紹了
Effective C++笔记(一)——条款26-29
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
條款26:盡可能延后變量定義式的出現時間
為何要盡量延后?
當程序中途跳出而導致變量未被使用,但是必須進行構造和析構。
最佳初始化變量
直接在構造時指定初值比構造之后再賦值效率高(條款4)
...std::string encrypted(password);...循環內變量定義在循環內還是循環外?
程序A:定義于循環外
//方法A:循環外定義POINT point;for (int i = 0; i < 1000000000; i++){point = tmp;//tmp = point;}程序B:定義于循環內
//方法B:循環內定義for (int i = 0; i < 1000000000; i++){POINT point(tmp);//tmp = point;}測試程序
int PostponeVariable(){POINT tmp;tmp.x = 12;tmp.y = 13;clock_t start = clock();//方法A:循環外定義POINT point;for (int i = 0; i < 1000000000; i++){point = tmp;//tmp = point;}clock_t end = clock();printf("A:%ld\n", (end - start));start = clock();//方法B:循環內定義for (int i = 0; i < 1000000000; i++){POINT point(tmp);//tmp = point;}end = clock();printf("B:%ld\n", (end - start));return 0;}結果
A:2953
B:2774
請按任意鍵繼續. . .
全部都是A比B執行的快,之前一直自以為是的覺得放在循環外效率比較高!
條款27:盡量少做轉型動作
條款28:避免返回handles指向對象的內部
class Point{ public: Point(int x, int y); ... void setX(int newVal); void setY(int newVal); ... }; struct RectData{ Point ulhc; Point lrhc; }; class Rectangle{ ... Point& upperLeft()const {return pData->ulhc;} Point& lowerRight()const {return pData->lrhc;} private: std::tr1::shared_ptr<RectData> pData; };const對象被修改:
Point coord1(0,0); Point coord2(100,100); const Rectangle rec(coord1, coord2); rec.upperLeft().setX(50);//現在rec變成從(50,0)到(100,100)修正:
class Rectangle{ ... const Point& upperLeft()const {return pData->ulhc;} const Point& lowerRight()const {return pData->lrhc;} private: std::tr1::shared_ptr<RectData> pData; };避免返回指向對象內部部件的句柄(引用、指針或迭代器)。這樣做可以增強封裝性,幫助 const 成員函數擁有更加“ const ”的行為,并且使“野句柄”出現的幾率降至最低。
條款29:為“異常安全”而努力
1. 修改菜單背景
class PrettyMenu{ public: ... void changeBackground(std::istream& imgSrc); ... private: Mutex mutex; Image* bgImage; int imageChanges; }; void PrettyMenu::changeBackground(std::istream& imgSrc) { lock(&mutex); delete bgImage; ++imageChanges; bgImage = new Image(imgSrc); unlock(&mutex); }2. 異常安全條件
- 不泄漏任何資源:若new Image(imgSrc)異常,則unlock永不解鎖
- 不允許數據破壞:若new Image(imgSrc)異常,則bgImage指向被刪除的指針,且imageChanges已被修改,導致未成功執行卻修改了數據。
3. 對象管理資源:解決資源泄漏
void PrettyMenu::changeBackground(std::istream& imgSrc) { Lock ml(&mutex);//來自條款14; delete bgImage; ++imageChanges; bgImage = new Image(imgSrc); }4. 異常安全函數保證
- 基本承諾:異常拋出時,保持有效狀態,沒有對象或數據結構被破壞
- 強烈保證:異常出現時,程序狀態不改變,成功則完全成功,失敗則會到之前狀態
- 不拋擲(nothrow)保證:承諾不拋出異常
5. 基本承諾
class PrettyMenu{ ... std::tr1::shared_ptr<Image> bgImage; ... };void PrettyMenu::changeBackground(std::istream& imgSrc) { Lock ml(&mutex); bgImage.reset(new Image(imgSrc)); ++imageChanges; }6. 強烈保證:copy and swap
struct PMImpl{ std::tr1::shared_ptr<Image> bgImage; int imageChanges; }; class PrettyMenu{ ... private: Mutex mutex; std::tr1::shared_ptr<PMImpl> pImpl; }; void PrettyMenu::changeBackground(std::istream& imgSrc) { using std::swap; Lock ml(&mutex); std::tr1::shared_ptr<PMImpl> pNew(new PMImpl(*pImpl)); pNew->bgImage.reset(new Image(imgSrc)); //修改副本 ++pNew->imageChanges; swap(pImpl, pNew);//置換數據 }轉載于:https://www.cnblogs.com/skywatcher/p/4045419.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Effective C++笔记(一)——条款26-29的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WordPress函数:wp_nav_m
- 下一篇: Oracle 的一些语句