Virtual Destructor

 

  Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor.
  Source: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP34-CPP.+Ensure+the+proper+destructor+is+called+for+polymorphic+objects

  For example, following program results in undefined behavior. Although the output of following program may be different on different compilers, when compiled using Dev-CPP, it prints following.
  Constructing base
  Constructing derived
  Destructing base

 1 // A program without virtual destructor causing undefined behavior
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 class base 
 7 {
 8 public:
 9     base()     
10     { 
11         cout<<"Constructing base \n"; 
12     }
13     ~base()
14     { 
15         cout<<"Destructing base \n"; 
16     }     
17 };
18 
19 class derived: public base 
20 {
21 public:
22     derived()     
23     { 
24         cout<<"Constructing derived \n"; 
25     }
26     ~derived()
27     { 
28         cout<<"Destructing derived \n"; 
29     }
30 };
31 
32 int main(void)
33 {
34     derived *d = new derived();  
35     base *b = d;
36     delete b;
37     getchar();
38     return 0;
39 }

 

  Making base class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both base class and derived class destructors are called.

  For example, following program prints:
  Constructing base
  Constructing derived
  Destructing derived
  Destructing base

 1 // A program without virtual destructor causing undefined behavior
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 class base 
 7 {
 8 public:
 9     base()     
10     { 
11         cout<<"Constructing base \n"; 
12     }
13     virtual ~base()
14     { 
15         cout<<"Destructing base \n"; 
16     }     
17 };
18 
19 class derived: public base 
20 {
21 public:
22     derived()     
23     { 
24         cout<<"Constructing derived \n"; 
25     }
26     ~derived()
27     { 
28         cout<<"Destructing derived \n"; 
29     }
30 };
31 
32 int main(void)
33 {
34     derived *d = new derived();  
35     base *b = d;
36     delete b;
37     getchar();
38     return 0;
39 }

 

  As a guideline, any time you have a virtual function in a class, you should immediately add a virtual destructor (even if it does nothing). This way, you ensure against any surprises later.

 

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  21:06:36

转载于:https://www.cnblogs.com/iloveyouforever/p/3444130.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法编写完整的软件系统。但是,以下是一个简单的教务系统的代码示例,可以作为参考: ``` class Student: def __init__(self, name, id, courses): self.name = name self.id = id self.courses = courses def add_course(self, course): self.courses.append(course) def remove_course(self, course): self.courses.remove(course) class Course: def __init__(self, name, id, instructor): self.name = name self.id = id self.instructor = instructor class Instructor: def __init__(self, name, id, courses): self.name = name self.id = id self.courses = courses def add_course(self, course): self.courses.append(course) def remove_course(self, course): self.courses.remove(course) class School: def __init__(self, students, instructors, courses): self.students = students self.instructors = instructors self.courses = courses def add_student(self, student): self.students.append(student) def remove_student(self, student): self.students.remove(student) def add_instructor(self, instructor): self.instructors.append(instructor) def remove_instructor(self, instructor): self.instructors.remove(instructor) def add_course(self, course): self.courses.append(course) def remove_course(self, course): self.courses.remove(course) ``` 这个代码示例定义了四个类:`Student`、`Course`、`Instructor`和`School`。`Student`和`Instructor`类都有一个`courses`属性,用于存储学生或教师所选的课程。`School`类有三个属性:`students`、`instructors`和`courses`,用于存储学校的学生、教师和课程。`School`类还有一些方法,用于添加或删除学生、教师或课程。 这个示例只是一个简单的模板,具体的教务系统功能需要根据实际需求进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值