【发布时间】:2012-05-16 00:21:38
【问题描述】:
我正在尝试解决程序中的一些问题,但我的复制构造函数或析构函数似乎存在问题。我遇到了内存异常。
如有任何帮助,我将不胜感激 谢谢
ArrayStorage::ArrayStorage(const ArrayStorage &a):readArray(a.readArray),arraysize(a.arraysize)
{
readArray = new string[arraysize]; //create the array
memcpy (readArray,a.readArray,sizeof(string)*arraysize);//Copy the values of bytes from the location pointed at by the souce and destination.
}
ArrayStorage::~ArrayStorage(void)
{
delete[](readArray);//deconstuctor to delete the array.
}
这会是复制数组而不是 memcpy 的更好方法吗:
for (int i = 0 ; i < arraysize ; i ++)
{
readArray[i] = a.readArray[i];
}
【问题讨论】:
标签: c++ arrays destructor copy-constructor