我在大学里用C ++进行了练习。所以他们要求我制作一个复制构造函数和一个重载赋值运算符=。所以我做到了,而且效果很好。他们说我在方法get(),复制构造函数和重载赋值运算符上错了。不幸的是,该练习效果很好。他们也没有要求main()。

#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';
}

最佳答案

您的副本分配应为:

  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修饰符问题与您的其他吸气剂相似。

关于c++ - C++复制构造函数,重载赋值运算符,方法get(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17073152/

10-11 23:04
查看更多