简体   繁体   English

复制构造函数和operator =(多个数组......)

[英]copying constructor and operator= (multiple arrays…)

i have to create a copy constructor and assignment operator= for Reg class. 我必须为Reg类创建一个复制构造函数和赋值运算符=。 But i have theese complicated structures and don't know how to copy it correctly. 但我有复杂的结构,不知道如何正确复制它。 So, Question is, how can i create copy constructor for class Reg - shallow copy? 所以,问题是,如何为类Reg创建复制构造函数 - 浅拷贝? Another question is, how should operator= look like - it should be a deep copy of 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?

}

in method Add - here i find correct place(misto) where should i place id - using binary search 在方法添加 - 在这里我找到正确的位置(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;

in method Ressetle - i find id - i have to find place where shall i add another info(city,street,date) and i create to it new TMoves: 在方法Ressetle - 我找到id - 我必须找到的地方,我将添加另一个信息(城市,街道,日期),我创建新的TMoves:

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;

This topic may be confusing, but my code is soo long and i am facing "only" to theese two problems with copying. 这个话题可能令人困惑,但我的代码太长了,而且我只面临着两个复制问题。 Thank you for your time and replies. 感谢您的时间和回复。

Don't give different kinds of semantics to copy constructor and copy assignment operator. 不要给复制构造函数和复制赋值运算符赋予不同类型的语义。

By default both should give a free-standing copy. 默认情况下,两者都应该提供一个独立的副本。

The best implementation of a copy constructor is to rely on copy construction of members and just use the compiler-generated one. 复制构造函数的最佳实现是依赖成员的复制构造,只使用编译器生成的构造。 To do this, use std::string instead of char* , and use std::vector for other arrays. 为此,请使用std::string而不是char* ,并将std::vector用于其他数组。 It's that simple. 就这么简单。


For the homework situation where you have been explicitly instructed to not use string and vector , define your own such classes. 对于已明确指示不使用stringvector作业情况,请定义自己的类。

Keep to the maxim that each class manages at most one resource, such as a dynamically allocated thingy. 保持每个类最多管理一个资源的格言,例如动态分配的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM