【发布时间】:2018-02-08 16:19:27
【问题描述】:
我正在创建一个跟踪学生的课程。在这个类中,我使用重载的 = 来复制这些学生对象。为了跟踪他们的课程,我使用了一个动态数组。数组复制得很好;但是,当清除学生对象的变量时,之前从该对象复制的任何对象也会擦除其数组。代码如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
string name; //Name
string* classList = NULL; //Empty array to store class names in
int numClasses = 0; //Number of classes
public:
void InputData()
{
cout << "Enter student name: " << endl; //Input Name
cin >> name;
cout << "Enter number of classes: " << endl; //Input classes
cin >> numClasses;
classList = new string[numClasses]; //Define array size
for (int i = 0; i < numClasses; i++) //For every spot in array, name class
{
cout << "Enter name of class " << (i + 1) << ":" << endl; //Name class
cin >> classList[i];
}
};
void OutputData()
{
cout << "Name: " << name << endl; //Output data
cout << "Number of Classes: " << numClasses << endl;
for (int i = 0; i < numClasses; i++) //Cycle through and output classes
{
cout << "Class " << i << ": " << classList[i] << endl;
}
};
void ResetClasses()
{
name = "";
delete[] classList; //Free Memory
classList = NULL; //Clear array
numClasses = 0;
};
Student operator =(Student& student) //Overload =
{
this->name = student.name;
this->classList = student.classList;
this->numClasses = student.numClasses;
return *this;
};
};
int main()
{
Student s1, s2;
s1.InputData(); // Input data for student 1
cout << "Student 1's data:" << endl;
s1.OutputData(); // Output data for student 1
s2 = s1;
cout << "Student 2's data after assignment from student 1:" << endl;
s2.OutputData(); // Should output same data as for student 1
s1.ResetClasses();
cout << "Student 1's data after reset:" << endl;
s1.OutputData(); // Should have no classes
cout << "Student 2's data, should still have original classes:" << endl;
s2.OutputData(); // Should still have original classes
}
主要的罪犯几乎可以肯定是这两个中的任何一个
void ResetClasses()
{
name = "";
delete[] classList; //Free Memory
classList = NULL; //Clear array
numClasses = 0;
};
Student operator =(Student& student) //Overload =
{
this->name = student.name;
this->classList = student.classList;
this->numClasses = student.numClasses;
return *this;
};
程序的输出是:
Enter student name:
ERIC
Enter number of classes:
2
Enter name of class 1:
C++
Enter name of class 2:
C
Student 1's data:
Name: ERIC
Number of Classes: 2
Class 0: C++
Class 1: C
Student 2's data after assignment from student 1:
Name: ERIC
Number of Classes: 2
Class 0: C++
Class 1: C
Student 1's data after reset:
Name:
Number of Classes: 0
Student 2's data, should still have original classes:
Name: ERIC
Number of Classes: 2
Class 0:
Class 1:
我做错了什么会导致这种情况?
【问题讨论】:
-
使用向量而不是原始指针,你会摆脱你的问题。目前您有 2 个具有相同指针的对象。
-
停止疯狂,使用
std::vector<std::string>。 -
如果 2 个 cmets 已经这样说还不够,您应该使用
vector<string>。不是原始数组和原始指针之类的东西。 -
this->classList = student.classList;是错的。请创建类列表的深层副本。当前解决方案的问题是,当您重置类时,它将释放内存,但另一个类正在使用相同的指针。这就是为什么你需要深拷贝。另一个与崩溃无关的错误是 operator= 应该返回 ref,即 student& 而不是 student。但正如其他人提到的,使用向量
-
要纠正您的代码的所有问题,您的代码中缺少主要部分(例如@SamVarshavchik 指出的)。 IMO 最好see a small example 这些操作以及您所缺少的..
标签: c++ arrays object dynamic operator-overloading