Do not forget each part of the object when copying C ++. Do not forget to copy the object.

Source: Internet
Author: User

Do not forget each part of the object when copying C ++. Do not forget to copy the object.

Ensure two points: 1. copy all member variables. 2. Call the copy function of all base classes.

class Customer{public:explicit Customer(const tstring _name,const tstring _lastTransaction):m_sName(_name), m_sLastTransaction(_lastTransaction){OutputDebugString(_T("Customer construct \n"));}virtual ~Customer(){OutputDebugString(_T("Customer deconstruct \n"));}Customer(const Customer& customer) : m_sName(customer.m_sName){OutputDebugString(_T("Customer copy construct \n"));}Customer& operator=(const Customer& customer){m_sName = customer.m_sName;OutputDebugString(_T("Customer operater \n"));return *this;}private:tstring m_sName;tstring m_sLastTransaction;};class  PriorityCustomer: public Customer{public:explicit PriorityCustomer(const tstring name,const tstring lastTime,int nPriority):m_nPriority(nPriority), Customer(name, lastTime){OutputDebugString(_T("PriorityCustomer construct \n"));}PriorityCustomer(const PriorityCustomer& rhs) :m_nPriority(rhs.m_nPriority),Customer(rhs){OutputDebugString(_T("PriorityCustomer copy construct\n"));}PriorityCustomer& operator=(const PriorityCustomer rhs){OutputDebugString(_T("PriorityCustomer operater \n"));m_nPriority = rhs.m_nPriority;Customer::operator=(rhs);return *this;}virtual ~PriorityCustomer(){OutputDebugString(_T("PriorityCustomer deconstruct \n"));}private:int m_nPriority;};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.