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





