Effective C++读书笔记之十二:复制对象时勿忘其每一个成分
Item 12:Copy all parts of an object
class Date{...}; class Customer { public: ... private: std::string name; Date lastTransaction; }; class PriorityCustomer:public Customer { public: PriorityCustomer(const PriorityCustomer& rhs); PriorityCustomer& operator=(const PriorityCustomer& rhs); ... private: int priority; }: PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs) :Customer(rhs),priority(rhs.priority) //调用base class的copy构造函数 {} PriorityCustomer& PriorityCustomer::operator = (const PriorityCustomer& rhs) { Customer::operator=(rhs); //对base class成分进行赋值动作 priority=rhs.priority; return *this; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。