动态数组、指针和复制构造函数的问题

Issues with dynamic arrays, pointers and copy-constructors

提问人: 提问时间:8/20/2020 最后编辑:Remy Lebeau 更新时间:8/20/2020 访问量:242

问:

我在创建包含对象的动态数组时遇到了很多问题。

据我了解,因为我的数组正在处理对象,所以存储在数组中的类必须具有复制构造函数或赋值运算符,以便正确复制所有内容。

我已经成功地创建了这个程序,其中包含定义大小的正常数组。现在,我在使用动态数组创建相同的程序时遇到了很多问题。

第 1 类 要存储的对象:

class objToBeStored{
private:
    string dataToBeStored;
    int sizeOfArray;
    string *storedArray;
    
public:
    objToBeStored(); //empty constructor
    objToBeStored& operator =(const objToBeStored& o); // assignment operator
    ~objToBeStored(); //destructor (no code inside);
    bool getData(istream &stream);
    //.....other methods to do stuff
};
    
objToBeStored::objToBeStored(){
    //empty
}

objToBeStored& objToBeStored::operator=(const objToBeStored& o){
    if(this != o){
        dataToBeStored = o.dataToBeStored;
        for (int i = 0; i < sizeOfArray; i++){
            storedArray[i] = o.storedArray[i];
        }
    }
    return *this;
}

void objToBeStored::getData(istream &stream){
    stream >> dataToBeStored >> sizeOfArray;
    storedArray = new string[sizeOfArray];
    for(int i = 0; i < sizeOfArray; i++){
        stream >> storedArray[i];
    }
    return !stream.eof();
}

//.....other methods to do stuff

类 2 包含存储上述对象的动态数组。一切都在工作,除了我如何声明我的动态数组和处理它的函数。因此,我将在下面编写以下代码:

class storageArrayClass{    
private:
    storageArrayClass *store;
    storageArrayClass *storptr;
    int numberOfstored;
    
public:
    storageArrayClass(); //empty constructor
    ~storageArrayClass();
    void addElm(objToBeStored & o);
    //other functions to do stuff    
};
    
storageArrayClass::storageArrayClass(){ //constructor
    numberOfstored = 0;
}

storageArrayClass::~storageArrayClass(){
}
    
void storageArrayClass(istream &stream) {
    
    objToBeStored o;
    o.getData(stream);
    
    if(numberOfstored == 0){ //check it this is the first element
        store = new objToBeStored[1];   //create a new array with length 1
        store[(numberOfstored] = o; //store object
    }else{
        objToBeStored tmpStore = new objToBeStored[(numberOfstored+1];  //create a temp. array with 1 more position
        for(int i=0; i < numberOfstored; i++){
            tmpStore[i] = store[i]; //copy original array to the temp. array
            storptr = &tmpStore[i]; // increment a point
        }
        
        storptr++; //increment pointer to last position
        *storptr = o; //store object in last position
        
        delete[] store; //delete the original array
        store = new objToBeStored[(numberOfstored+1]; //create a new original array
        store = tmpStore;//copy temp. array
    }
}

在出现以下错误之前,我设法将 3 个对象添加到我的动态数组中:

进程返回 -1073741819 (0xC0000005) 执行时间 : 5.059 s

请帮忙。我在这里阅读了无数的帖子,但我无法让它工作。

C++ 指针 复制构造函数 动态数组赋 值运算符

评论

2赞 Ben Voigt 8/20/2020
替换为,编译器将捕获许多剩余的错误。string *storedArray;std::unique_ptr<string[]>
2赞 Ben Voigt 8/20/2020
在你的复制构造函数中,如果 和 不相同,你就有一个大问题sizeOfArrayo.sizeOfArray
0赞 Jesper Juhl 8/20/2020
你为什么不只使用对象 - 而不是指向对象的指针?std::vector
1赞 PaulMcKenzie 8/20/2020
@NoviceJava 你为什么省略了写析构函数?你为什么省略了为?根据您向我们展示的内容,该程序很容易通过简单的 3 行程序失控,这都是由于类没有正确的复制语义。任何针对存根或缺失的析构函数运行的程序都会导致问题。您应该确保的第一件事是查看对象是否可以复制,然后担心以后添加条目。objToBeStoredstorageArrayClassmain
1赞 PaulMcKenzie 8/20/2020
向量是一个动态数组。您现在所做的所有工作都与 Vector 所做的完全相同,只是正确、高效和安全。向量不是一个在C++语言之外施展魔法的神秘类。它包装了您尝试执行的所有操作,并且正确地执行了操作。new[]delete[]

答: 暂无答案