简体   繁体   English

复制构造函数和重载赋值运算符的困难

[英]Difficulty with copy constructor and overloaded assignment operator

I am working on a problem to open a PGM file, make two copies of it, modify the original, and save all three separately.我正在解决一个问题,打开一个 PGM 文件,制作两个副本,修改原始文件,然后分别保存所有三个文件。 I am having a lot of trouble understanding how to write a copy constructor that accepts an array and also includes an overloaded assignment operator.我在理解如何编写一个接受数组并且还包含重载赋值运算符的复制构造函数时遇到了很多麻烦。 I also need to include a destructor so it follows the law of three, but each iteration I've done so far gives me an error for heap corruption.我还需要包含一个析构函数,以便它遵循三定律,但是到目前为止我所做的每次迭代都会给我一个堆损坏错误。

As of right now, my code modifies the original and exports it fine.截至目前,我的代码修改了原始代码并将其导出。 But my output files for the two unmodified copies only contain three zeroes for one and the first three lines for another.但是我的两个未修改副本的 output 文件只包含三个零,一个是前三行,另一个是前三行。

PGMImage::PGMImage(const PGMImage & anotherImage) {
  int i;
  pixels = new int[numberPixels + 1];
  for (i = 0; i < numberPixels; ++i) {
    pixels[i] = anotherImage.pixels[i];
  }
  cout << "Copy constructor called." << endl;
}

//Overloaded Assignment Operator
PGMImage & PGMImage::operator = (const PGMImage & anotherImage) {
  if (this != & anotherImage) {
    //delete pixels;                  
    pixels = new int[numberPixels];

    * pixels = * (anotherImage.pixels);
  }
  return *this;
}

//Destructor
PGMImage::~PGMImage() {}

I'm assuming that int* pixels and int numberPixels are private members of your class that represent the image data.我假设int* pixelsint numberPixels是代表图像数据的 class 的私有成员。 I'm going to also take a wild guess that you got other members of PGMImage such as width , height , and bitdepth .我还将大胆猜测您还有 PGMImage 的其他成员,例如widthheightbitdepth Both the assignment operator and the copy constructor need to copy all member variables over to the target object.赋值运算符和复制构造函数都需要将所有成员变量复制到目标 object。

Also, I don't know why you are doing a +1 on the memory allocation.另外,我不知道您为什么要对 memory 分配进行+1

I'm only using this-> for clarity to indicate a member on the target object and to distinguish it from anotherImage .为了清楚起见,我仅使用this->来指示目标 object 上的成员并将其与anotherImage区分开来。

Here's what I think you methods should look like:这是我认为你的方法应该是这样的:

void PGMImage::InitializeFromImage(const PGMImage& anotherImage) {
  this->pixels = new int[anotherImage.numberPixels];
  memcpy(this->pixels, anotherImage.pixels, sizeof(int)*anotherImage.numberPixels);
  this->numberPixels = anotherImage.numberPixels;

  this->width = anotherImage.width;
  this->height = anotherImage.height;
  this->bitdepth = anotherImage.bitdepth;
}

PGMImage::PGMImage(const PGMImage& anotherImage) {
  InitializeFromImage(anotherImage);
  cout << "Copy constructor called." << endl;
}

PGMImage& PGMImage::operator = (const PGMImage &anotherImage) {
  if (this != &anotherImage) {
      delete [] this->pixels;
      this->pixels = nullptr;
      InitializeFromImage(anotherImage);
  }
  return *this;
}

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

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