我在弄清楚如何编写我的复制构造函数时遇到了一些麻烦......
这是我的构造函数:
public:
Person(const char * aName, const char * userID, const char * domain, Date aDate) {
//This constructor should create heap objects
name = new char[strlen(aName) + 1];
strcpy (name, aName);
emailAddress = new EmailAddress(userID, domain);
birthday = new Date(aDate);
cout << "\nPerson(...) CREATING: ";
printOn(cout);
}
这就是我要为我的复制构造函数做的事情:
Person(const Person & p){
name = new char[strlen(p.name)+1];
strcpy(name, p.name);
emailAddress = new EmailAddress(*p.emailAddress);
birthday = new Date(*p.date);
cout << "\nPerson(const Person &) CREATING: ";
printOn(cout);
}
我不确定在我的复制构造函数中为我的新 Date 和新 EmailAddress 传递什么,我现在正在做的事情根本不起作用!
这是我的赋值运算符(我不知道在这里再次传递什么日期和电子邮件地址......):
Person & operator=(const Person & p) {
if(&p != this) {
delete [] name;
delete emailAddress;
delete birthday;
name = new char[strlen(p.name) + 1];
strcpy (name, p.name);
emailAddress = new EmailAddress();
birthday = new Date();
}
return *this;
}
任何帮助将不胜感激!
编辑:
日期定义
class Date{ //this class is complete
//This class represents a date
public:
Date(int aDay, int aMonth, int aYear) : day(aDay), month(aMonth), year(aYear) {}
Date(const Date & aDate) : day(aDate.day), month(aDate.month), year(aDate.year) {};
void printOn(ostream & out) const {out << day <<"/" << month << "/" << year;}