以下代码涉及4个类。
基础班是人班,有两个派生班学生和讲师。每个人都支持两个函数:toString()和type()。 Type()返回该类的名称,而toString()打印该实例的信息(学生或讲师)。
人是抽象类,但学生和讲师都是具体的类。
我已经实现了以上两个功能。
许多讲师将共享指向相同SalaryTable的指针,可能还有其他SalaryTables(支持相同的功能),并且讲师不拥有SalaryTable。另外,它们每个都有一些额外的功能。
谁能解释我的第三点?据我所知,我利用了Lecturer构造函数中提供的SalaryTable指针,并将其分配给了我在Lecturer.h中添加的SalaryTable指针(salaryTable_)。然后我使用salaryTable _-> annualSalary(grade_)返回薪水。然后在〜Lectuere()的析构函数中,我删除了salaryTable_。
这是正确的方法吗?当我这样做时,仅调用〜Salary()析构函数,而基类析构函数(〜Person())和派生类析构函数(〜Student()&〜Lecturer())均未调用。谁能解释我在哪里错了?
main.cpp
int main(int argc, char* argv[]) {
if (argc == 1) {
SalaryTable st;
Person* arr[2];
arr[0] = new Student("Apolo",5);
arr[1] = new Lecturer("Zeus","CO7100",33,&st);
for (unsigned int i=0 ; i<2 ; ++i) {
if (arr[i]->type() == "Student") {
Student* s=dynamic_cast<Student*>(arr[i]);
s->addMCF("blah blah");
s->addMCF("");
s->addMCF("Something else");
}
}
for (unsigned int i=0 ; i<2 ; ++i) {
cout << *arr[i] << endl;
}
}
}
工资表
#ifndef SALARYTABLE_H_
#define SALARYTABLE_H_
class SalaryTable {
public:
SalaryTable();
~SalaryTable();
unsigned int annualSalary(unsigned int grade) const;
};
#endif /* SALARYTABLE_H_ */
人
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iosfwd>
#include <vector>
#include "SalaryTable.h"
using std::vector;
using std::string;
class Person {
public:
Person() = delete;
Person(const Person&) = delete;
Person(Person&&) = delete;
Person(const char* name);
Person(const std::string& name);
virtual ~Person();
// Return the name of the Person
// Should be supported by all Persons.
std::string name() const;
virtual std::string toString() const=0;
virtual std::string type() const=0;
friend std::ostream& operator<<(std::ostream&, const Person&);
private:
std::string name_;
};
学生
class Student: public Person {
public:
Student() = delete;
Student(const Student&) = delete;
Student(Student&&) = delete;
Student(const char* name, unsigned int studentId);
Student(const std::string& name, unsigned int studentId);
virtual ~Student();
void addMCF(const std::string&);
std::string MCF(unsigned int);
unsigned int id() const;
std::string toString() const;
std::string type() const;
private:
unsigned int studentId_;
vector<string> vec_;
};
讲师
class Lecturer: public Person {
public:
Lecturer() = delete;
Lecturer(const Lecturer&) = delete;
Lecturer(Lecturer&&) = delete;
Lecturer(const char* name, const char* teaches, unsigned int grade,
SalaryTable*);
Lecturer(const std::string& name, const std::string& teaches,
unsigned int grade, SalaryTable*);
virtual ~Lecturer();
void increaseGrade();
unsigned int salary() const;
void changeModule(const std::string& newModule);
std::string teaches() const;
std::string toString() const;
std::string type() const;
private:
string teaches_;
string module_;
unsigned int grade_;
SalaryTable& salaryTable_;
};
#endif /* PERSON_H_ */
注意:请注意,我无法对.h文件进行更改。
我已经收到了很多有关销毁指针的意见。但我的最终问题是:为什么除SalaryTable类之外的所有其他类都没有被销毁。我通过在所有类的析构函数中打印一个stmt进行了验证。谁能给它一些启示。
“还添加了main.cpp文件,我也无法修改它。”
最佳答案
您必须确定谁拥有每个SalaryTable
的指针,每个Lecturer
在构造时都会收到一个指针。
有两个类似的选项。
(1)该表由另一个类/代码明确拥有(在您的问题中未显示),然后Lecturer
应保留对该表的观察指针(或引用)(const SalaryTable*const
或SalaryTable&
或类似名称)它从不习惯delete
,并且代码布局必须保证SalaryTable
在任何Lecturer
观察之前都不会被破坏。
(2)您可以使用std::shared_ptr
来确保后者,但这要付出一定的代价,并且还意味着SalaryTable
没有所有者,但是所有将shared_ptr
保留给其的代码部分都必须保留所有权。
对我来说(1)似乎更合乎逻辑:SalaryTable
是不依赖任何Lecturer
的基本对象,应在任何Lecturer
之前创建,并在任何Lecturer
之后销毁。如果您还没有经验,我也建议避免使用shared_ptr
。
关于c++ - 虚拟析构函数在基类和派生类中未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27668455/