gpt4 book ai didi

c++ - 如何使用 '=' 将一个类的对象复制到同一类的另一个对象

转载 作者:行者123 更新时间:2023-11-30 04:29:14 24 4
gpt4 key购买 nike

如何仅通过使用“=”将一个类的对象复制到同一类的另一个对象。我知道我们必须使运算符重载。这是我目前所拥有的

#include<iostream>
#include<conio.h>
#include<iomanip>

using namespace std;



class arr
{
public:
int *arr1;
int len;

arr& operator = (const arr& eq) //for copying two arrays. <--- my overloader
{
arr temp1(eq.len);
arr *pttemp;
int i=0;
//temp.arr1=new int[eq.len];
//temp.len = eq.len;
for(i = 0 ; i < eq.len ; i++)
{
temp1.arr1[i] = eq.arr1[i];
}
pttemp = &temp1;
return temp1;
};

friend istream& operator >> (istream& ist, arr & r)
{
static int i = 0;
int *arrNew;

if (i == r.len)
{
r.len *=2;
arrNew = new int[r.len]; // allocate the new array
for(int j = 0; j < r.len/2; j++)// copy the old array to the first half of the new array
arrNew[j] = r.arr1[j];// delete the old array
delete [] r.arr1;// let arr point to the new array and continue use arr
r.arr1 = arrNew;
delete arrNew;
}

ist>>r.arr1[i];
i++;

return ist;
}

arr() //initializing constructor
{
len = 5;
arr1 = new int[len];
};

arr(int size) //initializing constructor with args
{
len = size;
arr1 = new int[len];
};

arr(arr& a) : arr1(a.arr1) //copy constructor
{
arr1 = new int[len];
};

~arr() //delete constructor
{
delete arr1;
};



};




void main()
{
int size = 5,i,temp,trig = 0;
arr orig(size), asc(size), desc(size);

//generate random numbers for orig

for (i = 0 ; i < size ; i++)
{
orig.arr1[i] = rand();
}

//copy original set to asc and desc
asc = orig;
desc = orig;

//sorting ascending
for (i = 0 ; i < size-1 ; i++)
{
trig = 1;
if (asc.arr1[i] < asc.arr1[i+1])
{
temp = asc.arr1[i];
asc.arr1[i] = asc.arr1[i+1];
asc.arr1[i+1] = temp;
trig = 0;
}
if (trig = 1)
break;
if (i == size - 1)
{
i = 0;
}
}

//sorting descending
for (i = 0 ; i < size-1 ; i++)
{
trig = 1;
if (desc.arr1[i] > desc.arr1[i+1])
{
temp = desc.arr1[i];
desc.arr1[i] = desc.arr1[i+1];
desc.arr1[i+1] = temp;
trig = 0;
}
if (trig = 1)
break;
if (i == size - 1)
{
i = 0;
}
}

//printing
cout<<"Original Array: ";
for (i = 0 ; i < size ; i++)
{
cout<<orig.arr1[i]<<" ";
}
cout<<endl;
cout<<"Ascending Array: ";
for (i = 0 ; i < size ; i++)
{
cout<<asc.arr1[i]<<" ";
}
cout<<endl;

cout<<"Descending Array: ";
for (i = 0 ; i < size ; i++)
{
cout<<desc.arr1[i]<<" ";
}
cout<<endl;

getch();

}

它编译正确,但它('asc' 和 'desc')显示的数字与 'orig' 对象不同。

最佳答案

正确的解决方案是这样的:

struct Foo
{
std::vector<int> arr;
friend std::ifstream & operator>>(/*...*/);
};

隐式定义的赋值运算符已经完全满足您的需求,巧妙的代码重用是 C++ 编程(“库设计语言”)的核心。

如果你想手写,你必须自己复制:

struct Bar
{
unsigned int len;
int * arr;

Bar & operator=(Bar const & rhs)
{
len = rhs.len;
delete[] arr;
arr = new int[len];
for (unsigned int i = 0; i != len; ++i) { arr[i] = rhs.arr[i]; }
return *this;
}

Bar() : len(), arr() { }
// more constructors

Bar(Bar const &); // exercise: write this!

~Bar() { delete[] arr; }
};

不过,这是一个非常糟糕的想法,因为这种模式根本无法概括:它不是异常安全的——想象一下 for 循环中的一个拷贝抛出了一个异常(exception)。现在您已经丢失了原始数据泄漏的内存。

更好的解决方案是先分配一个临时缓冲区:

int * temp = new int[len];
for (...) temp[i] = rhs.arr[i];

delete[] arr;
arr = temp;

现在这段代码很快就会变得非常难看,想象一下你有不止一个这样的代码!

简而言之:使用std::vector

关于c++ - 如何使用 '=' 将一个类的对象复制到同一类的另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9528527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com