《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二维数组的方式,其 ... 
随机推荐
- 笨办法学Python(八)
			习题 8: 打印,打印 formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) print formatter % (&q ... 
- 816 Ambiguous Coordinates (many cases problem)
			https://www.cnblogs.com/Java3y/p/8846955.html -- link of the problem 816 IDEA: check the dot and int ... 
- bpclntcmd一条神奇的命令,解决新安装nbu客户端无法连接的问题 (屡试不爽神命令)
			1. bpclntcmd案例 bpclntcmd -clear_host_cache bpclntcmd – 测试 NetBackup 系统的功能,并在 NetBackup 客户端上启用光纤传输服务 ... 
- python 面向对象(二)--访问限制
			在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从前面Student类的定义来看,外部代码还是可以自由地修改一个实例的na ... 
- 2017.11.18  C语言的算法分析题目
			算法分析 1. 选定实验题目,仔细阅读实验要求,设计好输入输出,按照分治法的思想构思算法,选取合适的存储结构实现应用的操作. 2. 设计的结果应在Visual C++ 实验环境下实现并进行调试.(也可 ... 
- c++参数传递的三种方式
			一般来说C++中参数传递有三种方式:值传递.指针传递.引用传递 1.值传递——传值 值传递是最常见的一种参数传递的方式,但是对初学者来说也最容易出错.如下例: #include<iostream ... 
- 第23章 	I2C—读写EEPROM—零死角玩转STM32-F429系列
			第23章 I2C—读写EEPROM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ... 
- machine learning trends from nips14
			from John Platt, Deputy Managing Director and Distinguished Scientist at Microsoft Research http://b ... 
- java 基础词汇 必须 第九天
			Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ... 
- django+xadmin在线教育平台(十三)
			这个6-8对应对应6-11,6-12 拷入forgetpassword页面 书写处理忘记密码的view users/views.py # 用户忘记密码的处理view class ForgetPwdVi ... 
