Effective C++ 条款12
生活随笔
收集整理的這篇文章主要介紹了
Effective C++ 条款12
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
復(fù)制對(duì)象時(shí),勿忘其每個(gè)成分
作者在本節(jié)條款提醒我們,在多重繼承的情況下進(jìn)行copy或者copy assignment 的operator=的編寫時(shí),一定要考慮base 類部分?jǐn)?shù)據(jù)的初始化后者復(fù)制。
對(duì)照一下代碼:
class Cutsomer { …… private:string name;string telphone; };class PriorityCustomer:public Cutsomer { public:PriorityCustomer(){cout<<"PriorityCustomer Ctor"<<endl;}PriorityCustomer(const PriorityCustomer& rhs):priority(rhs.priority){cout<<"PriorityCustomer Copy Ctor"<<endl;}PriorityCustomer& operator=(const PriorityCustomer& rhs){cout<<"PriorityCustomer assign operator"<<endl;priority=rhs.priority;return *this;} private:int priority; };PriorityCustomer中的數(shù)據(jù)有下面
int priority;string name;string telphone;而真正copy或者copy assignment的時(shí)候僅僅處理了int priority;
我們能夠看到上面的代碼中忽視了base類部分的數(shù)據(jù)的處理。這時(shí)改動(dòng)代碼例如以下:
轉(zhuǎn)載于:https://www.cnblogs.com/jhcelue/p/7053749.html
總結(jié)
以上是生活随笔為你收集整理的Effective C++ 条款12的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmeter练习(5)关联升级版—For
- 下一篇: TypeError: HashUpdat