我有两个类,数据库和记录。
class Database {
private:
Record* head;
public:
Database(Record*);
Database();
Database(const Database&);
Database& operator= (const Database &data);
};
class Record {
public:
Record(std::string, std::string, int, int, std::string);
Record(const Record&);
Record();
Record* next;
};
现在当我这样做时
Database PM1, PM2;
//operations on PM1
PM2 = PM1;
所发生的只是将 PM1 中的值分配给 PM2。永远不会调用赋值重载。我不知道为什么会发生这种情况。我也尝试过调试,但从未输入过该功能。我究竟做错了什么?
编辑:这是重载函数,它可能不正确,但我还没有能够测试它,因为我什至无法让它运行。
Database& Database::operator= (const Database &data) {
if(this == &data)
return *this;
if(data.head == NULL) {
this->head = NULL;
return *this;
}
Record *curr1, *curr2;
curr1 = new Record(*(data.head));
this->head = curr1;
for(curr2 = data.head->next; curr1 != NULL && curr2 != NULL; curr1 = curr1->next) {
curr1->next = new Record(*curr2);
curr2 = curr2->next;
}
return *this;
}