《Cracking the Coding Interview》——第13章:C和C++——题目3
2014-04-25 19:42
题目:C++中虚函数的工作原理?
解法:虚函数表?细节呢?要是懂汇编我就能钻的再深点了。我试着写了点测大小、打印指针地址之类的代码,能起点管中窥豹的作用,从编译器的外部感受下虚函数表、虚函数指针的存在。
代码:
// 13.3 How does virtual function works in C++?
#include <cstring>
#include <iostream>
using namespace std; class A {
}; class B {
public:
void f() {cout << "class B" << endl;};
}; class C {
public:
C();
}; class D {
public:
virtual void f() {
cout << "class D" << endl;
};
}; class E: public D {
public:
void f() {
cout << "class E" << endl;
};
}; int main()
{
D *ptr = new E();
D d;
E e;
unsigned ui; cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
cout << sizeof(D) << endl;
cout << sizeof(E) << endl;
// The result is 1 1 1 4 4 on Visual Studio 2012.
ptr->f();
// class with no data member have to occupy 1 byte, so as to have an address.
// class with virtual functions need a pointer to the virtual function table.
printf("The address of d is %p.\n", &d);
printf("The address of e is %p.\n", &e);
printf("The address of d.f is %p.\n", (&D::f));
printf("The address of e.f is %p.\n", (&E::f)); memcpy(&ui, &d, );
printf("d = %X\n", ui);
memcpy(&ui, &e, );
printf("e = %X\n", ui);
memcpy(&ui, ptr, );
printf("*ptr = %X\n", ui); return ;
}
《Cracking the Coding Interview》——第13章:C和C++——题目3的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- 批处理实现虚拟WIFI
类似于360免费WiFi那个软件,可以PC发射WiFi(需要有无线网卡). 源码如下: @Echo off title Windows7 虚拟Wifi----By yllinux mode con c ...
- mysql:数据库保存时间的类型——int和datetime的区别
我们都知道,时间保存在数据库中,可以选择使用两种类型,一种是int,一种是datetime 那么,它们两个有什么区别呢?要怎么用呢? 现在和小仓鼠一起来探讨一下 1.int和datetime的使用区别 ...
- 如何在markdown中实现缩进,空格,制表符
参考 https://stackoverflow.com/questions/6046263/how-to-indent-a-few-lines-in-markdown-markup Markdown ...
- 引用类型(三):Function类型
一. Function类型函数实际上是对象.每个函数都是Function类型都实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象都指针.1.函数通常是使 ...
- unity简单例子
1. https://www.cnblogs.com/chengxuzhimei/p/4992106.html 2.https://www.cnblogs.com/GreenLeaves/p/7086 ...
- Python类型转换+序列操作+基本概念辨析速查手册
第一部分是Python语言中基础中的基础,根据网上资料,合并如下: 1.类型转换 int(x [,base]) 将x转换为一个整数 long(x [,base]) 将x ...
- python显示灰度图
import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...
- js 动态创建标记
innerHTML:一旦使用了这个属性,它的全部内容都要被替换掉.且不会返回任何对刚插入的内容的引用 与document.write()方法一样,innerHTML属性也是HTML专有属性,不能用于任 ...
- java基础 xml 使用dom4j解析 xml文件 servlet根据pattern 找到class
package com.swift.kaoshi; import java.io.File; import java.util.List; import java.util.Scanner; impo ...
- Python 初始—(项目 目录结构)
在os 包下,给出 import os ,os.path.abspath(__file__)获取当前文件的相对路径,os.path.dirname 获取当前文件所在的文件夹目录 print(os.pa ...