【发布时间】:2014-04-03 23:26:41
【问题描述】:
我在这段代码中遇到了我的类的析构函数问题。就是说从来没有分配过,但它应该是,我自己从来没有删除过它。这是代码的sn-ps:
#ifdef UNIT_TESTING_CONSTRUCTORS
//Test Constructors
cout << "Test constructors \nConctructor 1:\n";
Doctor testDoc1;
testDoc1.displayPatientArray();
cout << "\nConstructor 2:\n";
Doctor testDoc2(2);
testDoc2.displayPatientArray();
cout << "\nConstructor 3:\n";
//Implement more test cases below:
Doctor testDoc3("Wesley Cates");
testDoc3.displayPatientArray();
cout << "\nConstructor 4:\n";
Doctor testDoc4("Baylor Bishop", 3);
testDoc4.displayPatientArray();
#endif
Doctor::Doctor() : doctorName("need a name."), patientArraySize(100), numOfPatient(0) {
//Create a dynamic array for patients below:
//stringPtr_t* pArray;
stringPtr_t* patientArray;
patientArray = new stringPtr_t[patientArraySize];
还有班级:
typedef unsigned short ushort_t;
typedef string* stringPtr_t;
class Doctor {
private:
string doctorName;
stringPtr_t patientArray;
ushort_t patientArraySize;
ushort_t numOfPatient;
public:
Doctor();
Doctor(ushort_t patientArrayCapacity);
Doctor(string docName);
Doctor(string docName, ushort_t patientArrayCapacity);
bool addPatient(string patientName);
void displayPatientArray();
void resizePatientArray(ushort_t newArraySize);
string getDoctorName() const {return doctorName;}
ushort_t getNumOfPatient() const {return numOfPatient;}
ushort_t getArraySize() const {return patientArraySize;}
void setDoctorName(string docName) {doctorName.assign(docName);};
void emptyPatientArray() {numOfPatient = 0;}
Doctor& operator =(const Doctor& docSource);
~Doctor() {delete [] patientArray;}
};
【问题讨论】:
-
你有缩进和写可读代码的问题吗?
-
您的问题不清楚。请阅读How to Ask 和help center 了解如何提问。
-
如果您不提供minimal complete example,任何人都不太可能帮助您。
-
这可能还不是问题,但您可能还需要一个复制构造函数(C++ 规则三)。
标签: c++ destructor dynamic-memory-allocation