《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二维数组的方式,其 ...
随机推荐
- 【JavaScript 封装库】BETA 2.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- ACM Arabella Collegiate Programming Contest 2015 F. Palindrome 并查集
题目链接:http://codeforces.com/gym/100676/attachments 题意: 给一个字符串,有一些约束条件,两个位置要相同,有一些是问号,求最后有多少种方案回文? 分析: ...
- poj 2112 Optimal Milking 奶牛
题目链接:http://poj.org/problem?id=2112 题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用:并告诉了产奶机.奶牛之间的两两距离Dij(0<=i,j< ...
- 16、SpringBoot------整合MapStruct
开发工具:STS 前言: 前端提交往后端的数据,一部分是不需要存入数据库当中的: 后端从数据库中取出的数据,一部分是不可以交给用户的: 那么,po面向的是DB,vo面向的是客户端, mapstruct ...
- 5、SpringBoot+Mybatis整合------多对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/3baea10a3a1104bda815c20695 ...
- EBS并发管理器启动失败,系统暂挂,在重置计数器之前修复管理程序
今天EBS安装补丁之后,因为停并发管理器的时候,因为关闭EBS应用时,并发管理器没有在前台停止,就直接停了应用服务,导致启动时,并发管理器直接起不来了,使用adcmctl.sh也没有办法启动. 进入系 ...
- RHEL7.X安装12.2RAC时root.sh错误CLSRSC-400的解决方案
问题现象: [root@ora12c ghome]# /opt/oracle/ghome/root.sh Performing root user operation. The following e ...
- link链接外部样式表
一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...
- discuz 被入侵后,最可能被修改的文件
最近发现站点被黑了,现在还不知道是由系统漏洞导致的系统账户被攻陷,还是程序漏洞,文件被篡改.有一些敏感关键词(例如:赌博,电子路单)被恶意指向,点击搜索结果自动跳转到其他站点,而且是大量的,通过搜索“ ...
- JavaScript的变量命名规则和关键字的介绍
变量的名字 就像 人的名字一样,不能乱起. 你的代码不是只有你一个人看,变量既然是名字,那就这个名字就要有特殊的意义: 举个栗子:翠花,我们能从这个名字中得到什么信息?(这个 ...