如果你声明自己的copying函数,当你你的代码出错时编译器不会告诉你
void logCall(const string& funcName)
{
cout << funcName << endl;
}
class Customer
{
public:
Customer(const string& str) :name(str)
{
}
Customer(const Customer& rhs):name(rhs.name)
{
logCall("Customer copy Customer");
}
Customer& operator=(const Customer& rhs)
{
logCall("Customer copy assignment operator");
name = this->name;
return *this;
}
private:
string name;
};
Customer c("Hello");
Customer a(c);
class Date
{
}
class Customer
{
public:
private:
string name;
Date lastTransaction;
};
class PriorityCustomer :public Customer
{
public:
PriorityCustomer()
{
}
PriorityCustomer(const PriorityCustomer& rhs):priority(rhs.priority)
{
logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment constructor");
priority = rhs.priority;
return *this;
}
private:
int priority=1;
};
PriorityCustomer a;
PriorityCustomer b(a);
PriorityCustomer(const PriorityCustomer& rhs):priority(rhs.priority),Customer(rhs)
{
logCall("PriorityCustomer copy constructor");
}
PriorityCustomer& operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment constructor");
Customer::operator=(rhs);
priority = rhs.priority;
return *this;
}