2014-04-25 20:07

题目:为什么基类的析构函数必须声明为虚函数?

解法:不是必须,而是应该,这是种规范。对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构函数在自动调用的时候,不会调用基类的析构函数,这样就会造成资源未释放引起的内存泄漏。

代码:

 // 13.6 If a class is defined as base class, why must its destructor be declared "virtual"?
// Answer:
// If the destructor of base class is declared "virtual", it will be called when a derived class object is being destroyed.
// Otherwise it wouldn't. As base class may already have some allocated data or other resources, the destructor of basic class must be called to clear them up.
// Missing the base class destructor will leave out those data allocated for base class, causing memory leakage.
// Even though there're no dynamically allocated data, the base class should be declared "virtual", as a good practice of coding.
class A {
public:
virtual ~A() {};
}; class B: public A {
public:
~B() {};
}; int main()
{
A *ptr = new B();
delete ptr;
// Here ~B() will be called first, ~A() to follow.
return ;
}

《Cracking the Coding Interview》——第13章:C和C++——题目6的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

  9. 《Cracking the Coding Interview》——第13章:C和C++——题目10

    2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...

随机推荐

  1. 使用SSH密钥方式登录ubuntu Linux,指令(ssh-keygen 和 ssh-copy-id)

    实验目的 从myVM1(本地主机)上登录myVM2(远程主机).采用密钥方式,不输入密码. 测试环境 主机:window7 sp1 64位 专业版 虚拟机:VMware workstation 12 ...

  2. 网页打开速度优化——HTTP请求头及响应头

    no-cache:不缓存过期的资源 no-store:不缓存 最近看了<图解HTTP>这本书,书上讲到了这两者的区别: no-cache从字面意义上很容易误解为不缓存,但是no-cache ...

  3. pat甲级1085

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  4. Git由来

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...

  5. linux下composer+laravel随笔

    1.composer中文网:https://www.phpcomposer.com/   laravel中文网:https://d.laravel-china.org/ 2.composer是的作用是 ...

  6. IOS 制作常用代码的快捷方式

    输入可以变的对象或类型,要用<#name#>

  7. FYI-django数据库操作-外键

    我先定义两个模型,一个是作者,一个是作者出版的书籍,算是一对多的类型.   class Person(models.Model); name = models.CharField('作者姓名', ma ...

  8. BeanNameAware接口和BeanFactoryAware接口(转)

              迄今为止,所接触到的Bean都是“无知觉”的,就像黑客帝国中机械工厂里面“养殖”的人类,他们虽然能完成一定的功能,但是根本不知道自己在工厂(BeanFactory)中的代号(id) ...

  9. SpringBoot非官方教程 | 第二十五篇:2小时学会springboot

    转载请标明出处: http://blog.csdn.net/forezp/article/details/61472783 本文出自方志朋的博客 一.什么是spring boot Takes an o ...

  10. Java日志框架介绍

    一.序言 日志为系统的必不可少的一部分,通过输出的日志我们可以排查线上出现的各种问题,就像断案的线索一样.我们还可以通过日志数据分析用户的行为习惯做大数据分析. 二.日志框架分类及其历史 框架的种类: ...