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