C ++拷贝构造函数,重载赋值运算符,方法get()

时间:2013-06-12 18:54:30

标签: c++ operator-overloading copy-constructor

我在大学里用c ++练习。所以他们要求我做一个复制构造函数和一个重载赋值operator =的课程。所以我做了,它工作得很好。他们说我在methos get()和我的复制构造函数以及重载赋值运算符中错了。不幸的是,这项运动很好。他们也没有要求主()。

#include <iostream>

using namespace std;

class DIcourse{

private:
   int *ids;
   int numstudents;
   char *title;
   char *description;
public:
    DIcourse();
   ~DIcourse();
    DIcourse(const DIcourse &tmp);
    DIcourse operator=(const DIcourse &tmp);
    int const get_num();
    char const *get_title();
    char const *get_description();
    int const *get_ids();
    int set_num_ids(int num);
    char set_description(char *tmp);
    char set_title(char *temp);
};

DIcourse :: DIcourse(){
   ids=NULL;
   numstudents=0;
   title=new char[50];
   description=new char[200];
}

DIcourse :: ~DIcourse(){
   delete [] ids;
   delete [] title;
   delete [] description;
}

DIcourse :: DIcourse(const DIcourse& tmp){
   numstudents=tmp.numstudents;
   ids=new int[numstudents];
   for(int i=0;i<numstudents;i++){
      ids[i]=tmp.ids[i];
   }
   title=tmp.title;
   description=tmp.description;
}

DIcourse DIcourse::operator=(const DIcourse &tmp){
   delete [] ids;
   numstudents=tmp.numstudents;
   ids=new int[numstudents];
   for(int i=0;i<numstudents;i++){
      ids[i]=tmp.ids[i];
   }
   delete[] description;
   delete[] title;
   description= new char[200];
   title= new char[50];
   description=tmp.description;
   title=tmp.title;
   return *this;
 }

const int DIcourse :: get_num(){
  return numstudents;
} 

const char* DIcourse :: get_title(){
   return title;
}

const char* DIcourse :: get_description(){
    return description;
}

const int* DIcourse :: get_ids(){
   return ids;
}

int DIcourse :: set_num_ids(int num){
   numstudents++;
   int *temp_array;
   temp_array=new int[numstudents];
   temp_array[numstudents-1]=num;
   if(ids!=NULL){
     for(int i=0; i<numstudents-1; i++){
       temp_array[i]=ids[i];
    }
   delete [] ids;
   }
   ids=temp_array;
}

 char DIcourse :: set_description(char *tmp){
   int i=0;
   while(tmp[i]!='\0'){
       description[i]=tmp[i];
       i++;
   }
   description[i]='\0';
}

char DIcourse :: set_title(char *temp){
   int i=0;
   while(temp[i]!='\0'){
       title[i]=temp[i];
       i++;
   }
   title[i]='\0';
} 

2 个答案:

答案 0 :(得分:4)

您的副本分配应为:

  DIcourse& operator=(const DIcourse &tmp);
 //^^return reference

同时,您应该检查copy assignment operator self-assignment

由@Mooing Duck提供:您当前的复制赋值运算符实现中存在内存泄漏:

  DIcourse DIcourse::operator=(const DIcourse &tmp){
          delete [] ids;
          numstudents=tmp.numstudents;
          ids = new int[numstudents];
          for(int i=0;i<numstudents;i++){
              ids[i] = tmp.ids[i];
          }
          delete[] description;
          delete[] title;
          description= new char[200]; //^^
          title= new char[50]; //^^^memory leak here and above
          description=tmp.description; //^^this is not copy char*
          title=tmp.title; //^^this is not copy char* 
          return *this;
     }
 }

您可能需要查看Copy-And-Swap Trick以实现复制赋值运算符。

您的get函数应为:

int  get_num() const;
              //^^const modifier should be here, 
             //meaning that it access the value but does not change it

const修饰符问题与其他getter类似。

答案 1 :(得分:1)

除了taocp提到的内容之外,您还需要检查您未分配给自己的赋值运算符。

DIcourse &DIcourse::operator=(const DIcourse &tmp){

if (this != &tmp) {
   ... Do the business here

}
return *this;
}