简体   繁体   English

C ++复制构造函数和赋值运算符定义

[英]C++ Copy Constructor and Assignment Operator Define

C++ Copy Constructor and Assignment Operator Define C ++复制构造函数和赋值运算符定义

Could anybody help me correct the following copy constructor and assignment operator? 有人可以帮助我更正以下副本构造函数和赋值运算符吗?

  1. as you see, assignment operator seems to work well; 如您所见,赋值运算符似乎运行良好; I ran it and it works. 我运行了它并且有效。 Do I define the assignment operator correct? 我定义的赋值运算符正确吗? Please let me know. 请告诉我。

  2. This crashes with the copy constructor... How can I fix the copy constructor? 复制构造器崩溃...如何修复复制构造器?

Please help me. 请帮我。

 #include <iostream>
 using namespace std;

 class IntP
 {
 private:
    unsigned int* counts;
    unsigned int numP;
    unsigned int size;

 public:
    IntP(); // Default Constructor
    IntP(int n); // Constructor
    IntP(const IntP& a); // Copy Constructor
    IntP& operator= (const IntP& a); // Assignment Operator
    ~IntP(); // Destructor
    void printIntP() const;
 };

 IntP::IntP() // Default Constructor
 {
   counts = new unsigned int[101] (); // initialize array of size 101 to all 0s
   numP = 0;
   size = 101;
 }

 IntP::IntP(int n) // Constructor
 {
   counts = new unsigned int[n+1] (); // initialize array of size n+1 to all 0s
   counts[n] = 1;
   numP = 1;
   size = n+1;
 }

 // ????????????
 // 
 IntP::IntP(const IntP& a) // Copy Constructor
 {
   this->size = a.size;
   this->numP = a.numP;
   for (int i=0; i < (int) this->size; i++)
      this->counts[i] = a.counts[i]; 
 }

 // ??????????? 
 // Correct Implementation?
 // without delete operator, we have memory leak? but it compiles without error??? 
 IntP& IntP::operator= (const IntP& a) // Assignment Operator
 {
   if (this != &a)
   {
     delete [] counts; // Get rid of old counts
     size = a.size; 
     numP = a.numP;
     counts = new unsigned int[size+1];
     counts[size] = 1;
     for (int i=0; i < (int) this->size; i++)
        counts[i] = a.counts[i]; 
   }
   return *this;
 }

 IntP::~IntP() { delete [] counts; }

 void IntP::printIntP() const
 {
    cout << "The size of array is " << this->size << endl;
    cout << "The numP variable becomes " << this->numP << endl;

    int i = 0;
    while ( i != (int) this->size )
    {
      cout << counts[i];
      if ( i != (int) this->size-1 ) cout << " , ";
      i++;
    } 

    cout << endl << endl;
 }

 int main (void)
 {

 IntP ip2(200); 
 IntP ip3; 
 ip3 = ip2;



 IntP ip1(100); 

 cout << "Print out   ip1 object  after IntP ip1(100); " << endl; 
 ip1.printIntP(); 


 IntP ip4(ip1); 

 cout << "Print out   ip4 object  after IntP ip4(ip1); " << endl; 
 ip4.printIntP();

 system("pause"); return 0;
 }

Your code crashes because of you don't allocate memory for counts in copy constructor. 因为您没有为复制构造函数中的计数分配内存,所以代码崩溃。

 IntP::IntP(const IntP& a) // Copy Constructor
 {
   //counts = new unsigned int[a.size] (); // Add this to allocate memory for counts
   this->size = a.size;
   this->numP = a.numP;
   for (int i=0; i < (int) this->size; i++)
      this->counts[i] = a.counts[i]; //counts is unitialized
 }

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

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