Effective C++筆記:復(fù)制對(duì)象時(shí)勿忘其每一個(gè)成分
設(shè)計(jì)良好之面向?qū)ο笙到y(tǒng)(OO-systems )會(huì)將對(duì)象的內(nèi)部封裝起來(lái),只留兩個(gè)函數(shù)負(fù)責(zé)對(duì)象拷貝(復(fù)制),那便是帶著適切名稱的copy構(gòu)造函數(shù)和copy assignment操作符,我稱它們?yōu)閏opying 函數(shù)。編譯器會(huì)在必要時(shí)候?yàn)槲覀兊腸lasses創(chuàng)建copying 函數(shù),并說(shuō)明這些"編譯器生成版"的行為: 將被拷對(duì)象的所有成員變量都做一份拷貝。
? ? ? ?如果你聲明自己的copying 函數(shù),意思就是告訴編譯器你并不喜歡缺省實(shí)現(xiàn)中的某些行為。編譯器仿佛被冒犯似的,會(huì)以一種奇怪的方式回敬:當(dāng)你的實(shí)現(xiàn)代碼幾乎必然出錯(cuò)時(shí)卻不告訴你。
? ? ? ?考慮一個(gè)class 用來(lái)表現(xiàn)顧客,其中手工寫出(而非由編譯器創(chuàng)建)copying 函數(shù),使得外界對(duì)它們的調(diào)用會(huì)被志記Clogged) 下來(lái):
void?logCall(const?std::string&?funcName);//?制造一個(gè)log?entry?? class?Customer?{?? public:?? ????......?? ????Customer(const?Customer&?rhs);?? ????Customer&?operator=(const?Customer&?rhs);?? ????......?? private:?? ????std::string?name;?? };?? Customer::Customer(const?Customer&?rhs):name(rhs.name)//?復(fù)制rhs?的數(shù)據(jù)?? {?? ????logCall("Customer?copy?constructor");?? }?? ?? Customer&?Customer::operator=(const?Customer&?rhs)?? {?? ????logCall("Customer?copy?assignment?operator");?? ????name?=?rhs.name;?//?復(fù)制rhs?的數(shù)據(jù)?? ????return?*this;???? }
? ? ? ?這里的每一件事情看起來(lái)都很好,而實(shí)際上每件事情也的確都好,直到另一個(gè)成員變量加入戰(zhàn)局:
class?Date?{?...?};//?日期?? class?Customer?{?? public:?? ????......?????????//?同前?? private:?? ????std::string?name;?? ????Date?lastTransaction;?? };
? ? ? ?這時(shí)候既有的copying函數(shù)執(zhí)行的是局部拷貝(partial copy) :它們的確復(fù)制了顧客的name,但沒有復(fù)制新添加的lastTransaction。大多數(shù)編譯器對(duì)此不出任何怨言一一即使在最高警告級(jí)別中。這是編譯器對(duì)"你自己寫出copying函數(shù)"的復(fù)仇行為: 既然你拒絕它們?yōu)槟銓懗鯿opying函數(shù),如果你的代碼不完全,它們也不告訴你。結(jié)論很明顯:如果你為class 添加一個(gè)成員變量,你必須同時(shí)修改copying函數(shù)。(你也需要修改class 的所有構(gòu)造函數(shù)以及任何非標(biāo)準(zhǔn)形式的operator=。如果你忘記,編譯器不太可能提醒你。)
? ? ? ?一旦發(fā)生繼承,可能會(huì)造成此一主題最暗中肆虐的一個(gè)潛藏危機(jī)。試考慮:
class?PriorityCustomer:?public?Customer?{//?一個(gè)derivedclass?? public:?? ????......?? ????PriorityCustomer(const?PriorityCustomer&?rhs);?? ????PriorityCustomer&?operator=(const?PriorityCustomer&?rhs);?? ????......?? private:?? ????int?priority;?? };?? PriorityCustomer::PriorityCustomer(const?PriorityCustomer&?rhs)?? :?priority(rhs.priority)?? {?? ????logCall("PriorityCustomer?copy?constructor");?? }?? PriorityCustomer&?? PriorityCustomer::operator=(const?PriorityCustomer&?rhs)?? {?? ????logCall("PriorityCustomer?copy?assignment?operator");?? ????priority?=?rhs.priority;?? ????return?*this;?? }
? ? ? ?PriorityCustomer 的copying 函數(shù)看起來(lái)好像復(fù)制了PriorityCustomer 內(nèi)的每一樣?xùn)|西,但是請(qǐng)?jiān)倏匆谎?。是的,它們?fù)制了PriorityCustomer 聲明的成員變量,但每個(gè)PriorityCustomer還內(nèi)含它所繼承的Customer 成員變量復(fù)件(副本) ,而那些成員變量卻未被復(fù)制。PriorityCustomer 的copy 構(gòu)造函數(shù)并沒有指定實(shí)參傳給其base class 構(gòu)造函數(shù)(也就是說(shuō)它在它的成員初值列( member initialization list) 中沒有提到Customer) ,因此PriorityCustomer對(duì)象的Customer成分會(huì)被不帶實(shí)參之Customer 構(gòu)造函數(shù)(即default構(gòu)造函數(shù)必定有一個(gè)否則無(wú)法通過(guò)編譯)初始化。default構(gòu)造函數(shù)將針對(duì)name 和lastTransaction執(zhí)行缺省的初始化動(dòng)作。
? ? ? ?任何時(shí)候只要你承擔(dān)起"為derived class 撰寫copying 函數(shù)"的重責(zé)大任,必須很小心地也復(fù)制其base class 成分。那些成分往往是private ?,所以你無(wú)法直接訪問(wèn)它們,你應(yīng)該讓derived class 的copying 函數(shù)調(diào)用相應(yīng)的base class 函數(shù):
PriorityCustomer::PriorityCustomer(const?PriorityCustomer&?rhs)?? :?Customer?(rhs)?,?//?調(diào)用base?class的copy構(gòu)造函數(shù)?? ??priority(rhs.priority)?? {?? ????logCall("PriorityCustomer?copy?constructor");?? }?? PriorityCustomer&?? PriorityCustomer::operator=(const?PriorityCustomer&?rhs)?? {?? ????logCall("PriorityCustomer?copy?assignment?operator");?? ????Customer::operator=(rhs);?//?對(duì)base?class成分進(jìn)行賦值動(dòng)作?? ????priority?=?rhs.priority;?? ????return?*this;?? }
? ? ? ?本條款題目所說(shuō)的"復(fù)制每一個(gè)成分"現(xiàn)在應(yīng)該很清楚了。當(dāng)你編寫一個(gè)copying函數(shù),請(qǐng)確保(1) 復(fù)制所有l(wèi)ocal 成員變量, (2) 調(diào)用所有base classes 內(nèi)的適當(dāng)?shù)腸opying 函數(shù)。
? ? ? ?這兩個(gè)copying 函數(shù)往往有近似相同的實(shí)現(xiàn)本體,這可能會(huì)誘使你讓某個(gè)函數(shù)調(diào)用另一個(gè)函數(shù)以避免代碼重復(fù)。這樣精益求精的態(tài)度值得贊賞,但是令某個(gè)copying函數(shù)調(diào)用另一個(gè)copying 函數(shù)卻無(wú)法讓你達(dá)到你想要的目標(biāo)。
? ? ? ?令copy assignment 操作符調(diào)用copy 構(gòu)造函數(shù)是不合理的,因?yàn)檫@就像試圖構(gòu)造一個(gè)已經(jīng)存在的對(duì)象。這件事如此荒謬,乃至于根本沒有相關(guān)語(yǔ)法。是有一些看似如你所愿的語(yǔ)法,但其實(shí)不是:也的確有些語(yǔ)法背后真正做了它,但它們?cè)谀承┣闆r下會(huì)造成你的對(duì)象敗壞,所以我不打算將那些語(yǔ)法呈現(xiàn)給你看。單純地接受這個(gè)敘述吧:你不該令copy assignment 操作符調(diào)用copy 構(gòu)造函數(shù)。
? ? ? ?反方向一一令copy 構(gòu)造函數(shù)調(diào)用copy assignment 操作符一一同樣無(wú)意義。構(gòu)造函數(shù)用來(lái)初始化新對(duì)象,而copy assignment 操作符只施行于己初始化對(duì)象身上。對(duì)一個(gè)尚未構(gòu)造好的對(duì)象賦值,就像在一個(gè)尚未初始化的對(duì)象身上做"只對(duì)己初始化對(duì)象才有意義"的事一樣。無(wú)聊嘛!別嘗試。
? ? ? ?如果你發(fā)現(xiàn)你的copy 構(gòu)造函數(shù)和copy assignment 操作符有相近的代碼,消除重復(fù)代碼的做法是,建立→個(gè)新的成員函數(shù)給兩者調(diào)用。這樣的函數(shù)往往是private 而且常被命名為init。這個(gè)策略可以安全消除copy 構(gòu)造函數(shù)和copy assignment 操作符之間的代碼重復(fù)。
需要記住的
1.Copying 函數(shù)應(yīng)該確保復(fù)制"對(duì)象內(nèi)的所有成員變量"及"所有base class 成分"。
2.不要嘗試以某個(gè)copying 函數(shù)實(shí)現(xiàn)另一個(gè)copying 函數(shù)。應(yīng)該將共同機(jī)能放進(jìn)第三個(gè)函數(shù)中,并由兩個(gè)coping 函數(shù)共同調(diào)用。