我必须为 Reg 类创建一个复制构造函数和赋值 operator=。但我有这些复杂的结构,不知道如何正确复制它。所以,问题是,我如何为 Reg 类创建复制构造函数 - 浅拷贝?另一个问题是, operator= 应该是什么样子 - 它应该是 Reg 的深层副本
struct TMoves
{
const char* ddate;
const char* sstreet;
const char* ccity;
public:
~TMoves()
{
delete [] ccity;
delete [] ddate;
delete [] sstreet;
}
};
struct TData
{
int stackmult;
const char* iid;
const char* nname;
const char* ssurname;
int pocet;
TMoves** moves;
public:
TData()
{
stackmult=1;
}
~TData()
{
delete [] iid;
delete [] nname;
delete [] ssurname;
for(int i=0;i<pocet;i++)
{
delete moves[i];
}
delete [] moves;
}
};
class Reg
{
public:
Reg ();
Reg (const Regr&);
~Reg();
Reg& operator= (const Reg &);
bool Add (const char* id, const char* name, const char* surname, const char* date, const char* street, const char* city );
bool Resettle ( const char* id, const char* date, const char* street, const char* city );
private:
static const int MAX=1000; //default lenght of pole
TData **pole;
int counter; // pole lenght counter - not important now
int multiplier; // used for realocating pole
};
Reg::Reg()
{
counter=0;
multiplier=1;
pole=new TData*[multiplier*MAX];
}
Reg::Reg(const Reg& out)
{
//... how?
}
Reg::Reg &operator= (const Reg& copy)
{
//... how?
}
在方法中添加 - 在这里我找到正确的位置(misto)我应该在哪里放置 id - 使用二进制搜索
int misto=counter;
pole[misto]=new TData;
char *temp = new char[12];
strcpy(temp, id);
pole[misto]->iid = temp;
temp = new char[strlen(name)+1];
strcpy(temp, name);
pole[misto]->nname = temp;
temp = new char[strlen(surname)+1];
strcpy(temp, surname);
pole[misto]->ssurname = temp;
pole[misto]->moves=new TMoves*[STAT];
pole[misto]->moves[0]=new TMoves;
temp = new char[strlen(city)+1];
strcpy(temp,city);
pole[misto]->moves[0]->ccity= temp;
temp = new char[strlen(date)+1];
strcpy(temp,date);
pole[misto]->moves[0]->ddate= temp;
temp = new char[strlen(street)+1];
strcpy(temp,street);
pole[misto]->moves[0]->sstreet= temp;
在方法 Ressetle - 我找到 id - 我必须找到我应该添加另一个信息(城市、街道、日期)的地方,并为它创建新的 TMove:
pole[misto]->moves[misto2]=new TMoves;
char *temp = new char[strlen(city)+1];
strcpy(temp,city);
pole[misto]->moves[misto2]->ccity= temp;
temp = new char[strlen(date)+1];
strcpy(temp,date);
pole[misto]->moves[misto2]->ddate= temp;
temp = new char[strlen(street)+1];
strcpy(temp,street);
pole[misto]->moves[misto2]->sstreet= temp;
这个话题可能令人困惑,但我的代码太长了,我“只”面临这两个复制问题。感谢您的时间和答复。