【发布时间】:2019-10-05 01:18:19
【问题描述】:
我正在尝试将动态分配的数组复制到实例。我的代码似乎正在复制这些值,但它还需要调整数组的大小以匹配“&other”大小的数组。
关于代码的一些信息:手头有两个类,一个是“电影”,它以标题、电影时间和导演(所有指针)作为私有成员。还有一个叫做“MovieCollection”的数组,它是一个数组,将“Movie”的每个实例存储在给定的索引中。
//These are private member variables:`
int ArrySize = 50; //There is another section of code that points to this and resizes if needed, I believe it needed a size at runtime though.
//Array to store instance of "movie"
Movie *movieArry = new Movie[ArrySize];
//This is assignment operator
const MovieCollection& operator=(const MovieCollection& other)
{
delete []movieArray;
int otherSizeArry = other.ArrySize;
Movie* temp;
temp = new Movie[otherSizeArry];
for (int i = 0; i < otherSizeArry; i++)
temp[i] = other.movieArry[i];
return *this;
delete []temp;
}
在创建实例时,我使用了我编写的另一个函数来调整数组的大小。例如,我要复制的实例有 10 个索引,但我尝试将值复制到的新实例仍然有 50 个限制。据我了解,我必须删除它,因为数组无法调整大小,然后复制新的大小(连同值)。
任何帮助将不胜感激,并在此先感谢您。另外,如果需要更多代码,请见谅。我不想提供超出需要的东西。
【问题讨论】:
-
只需使用
std::vector。尝试手动管理这样的阵列是没有意义的。代码也存在多个问题,但很难评论,因为它不像发布的那样有效/可编译。您正在删除movieArray before 复制它(未定义的行为和use-after-free),并在返回后尝试删除temp,它永远不会被执行(泄漏),并且您永远不会分配复制的数组到任何东西。 -
在赋值运算符中,
movieArray被删除后需要赋值给某物。如movieArray = temp在return语句之前。如果您没有正确编码,这些事情就不会发生。
标签: c++ arrays dynamic-arrays delete-operator