【发布时间】:2015-02-24 10:25:28
【问题描述】:
以下代码涉及 4 个类。
基类是 Person 类,有两个派生类 Student 和 Lecturer。每个人都支持两个函数:toString() 和 type()。 Type() 返回该类的名称,而 toString() 打印实例的信息(学生或讲师)。
Person 是抽象类,但 Student 和 Lecturer 都是具体类。
以上两个功能我已经实现了。
- 许多讲师将共享指向同一个 SalaryTable 的指针,可能还有其他 SalaryTable(支持相同的功能)并且讲师不拥有 SalaryTable。此外,它们每个都有一些额外的功能。
谁能单独解释第三点?据我所知,我使用了 Lecturer 构造函数中给出的 SalaryTable 指针,并将其分配给了我在 Lecturer.h 中添加的 SalaryTable 指针 (salaryTable_)。然后我使用salaryTable_->annualSalary(grade_) 返回薪水。在 ~Lectuere() 的析构函数中,我做了一个 deletesalaryTable_。
这样做是否正确?当我这样做时,只会调用 ~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;
}
}
}
SalaryTable.h
#ifndef SALARYTABLE_H_
#define SALARYTABLE_H_
class SalaryTable {
public:
SalaryTable();
~SalaryTable();
unsigned int annualSalary(unsigned int grade) const;
};
#endif /* SALARYTABLE_H_ */
人.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_;
};
学生.h
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_;
};
讲师.h
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 文件,我也无法修改它。”
【问题讨论】:
-
有问题的代码在哪里?为什么你认为我们有心灵感应?
-
问题的标题可能有点误导。我想真正的问题涉及在每个
Lecturer中处理SalaryTable的所有权。 -
由于
Lecturer类不拥有指向SalaryTable的指针,它应该不删除它。 -
@Cheers 和 hth。 -Alf,我也添加了实现代码
-
@DhiwaTdG:这很好,但出现问题的代码是使用这些类的代码。猜测代码具有
Person的向量或数组。但在你展示它之前,它需要心灵感应的力量才能确定。发布代码示例的简单规则是(在可能的情况下)读者应该能够编译并运行它并查看问题。我可以编译并运行您发布的代码吗?
标签: c++ inheritance c++11 destructor virtual-functions