13.3 C++中的虚函数是如何工作的?

解答

虚函数依赖虚函数表进行工作。如果一个类中,有函数被关键词virtual进行修饰, 那么一个虚函数表就会被构建起来保存这个类中虚函数的地址。同时, 编译器会为这个类添加一个隐藏指针指向虚函数表。如果在派生类中没有重写虚函数, 那么,派生类中虚表存储的是父类虚函数的地址。每当虚函数被调用时, 虚表会决定具体去调用哪个函数。因此,C++中的动态绑定是通过虚函数表机制进行的。

当我们用基类指针指向派生类时,虚表指针vptr指向派生类的虚函数表。 这个机制可以保证派生类中的虚函数被调用到。

class Shape{
public:
int edge_length;
virtual int circumference () {
cout<<"Circumference of Base Classn";
return ;
}
};
class Triangle: public Shape{
public:
int circumference () {
cout<<"Circumference of Triangle Classn";
return * edge_length;
}
};
int main(){
Shape *x = new Shape();
x->circumference(); // prints “Circumference of Base Class”
Shape *y = new Triangle();
y->circumference(); // prints “Circumference of Triangle Class”
return ;
}

在上面的代码中,circumference是shape类的虚函数,因此在所有继承shape类的子类里都为虚函数。在C++里,非虚函数的调用时在编译器通过静态绑定确定的,而虚函数的调用则是在运行期通过动态绑定确定的。

careercup-C和C++ 13.3的更多相关文章

  1. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  4. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  5. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  6. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

  7. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  8. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  9. [CareerCup] 13.7 Node Pointer 节点指针

    13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...

  10. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

随机推荐

  1. FJOI2007轮状病毒

    不会推公式…… 不会基尔霍夫矩阵…… 不会matrix—tree定理…… 膜拜vfleaking大神…… 题解:http://z55250825.blog.163.com/blog/static/15 ...

  2. 【JSP】让HTML和JSP页面不缓存从Web服务器上重新获取页面

    用户退出后,如果点击浏览器上的后退按钮,Web应用将不能正确保护受保护的页面——在Session销毁后(用户退出)受保护的JSP页重新在浏览器中显示出来. 然而,如果用户点击返回页面上的任何链接,We ...

  3. [面试题] BloomFilter 无序40亿不重复 uint 整数, 给予任意的数,求是否在这40亿之中 + 无序数组中找2个相同的值

    一道百度面试题(待解中) 具体:给40亿个不重复的unsigned int的整数,没排过序的,然后再给几个数,如何快速判断这几个数是否在那40亿个数当中? 分析下,首先应该是空间复杂度(40亿uint ...

  4. 《Python基础教程(第二版)》学习笔记 -> 第五章 条件、循环 和 其他语句

    条件和条件语句 下面的值在作为布尔表达式的时候,会被解释器看作假(False):False None    0    ""    ()    []    {} 条件执行和if语句 ...

  5. Python脚本控制的WebDriver 常用操作 <二十三> wait

    测试用例场景 Wait类的使用场景是在页面上进行某些操作,然后页面上就会出现或隐藏一些元素,此时使用Wait类的until方法来等待这些效果完成以便进行后续的操作.另外页面加载时有可能会执行一些aja ...

  6. 把之前写的几个项目放到了github上

    之前有的源码放在我的电脑里不知道什么时候就没了,满满都是回忆啊,怪可惜的. https://github.com/redclock/Adv-Game:一个java游戏 https://github.c ...

  7. codeforces 354 DIV2

    B - Pyramid of Glasses n层杯子,问k分钟能流满多少个杯子?和到香槟一样的过程? 思路:应为水的流速为每分钟一立方体(YY),可以做个转化,把最上层的杯子最原始的容积看成K,每个 ...

  8. 基于野火M3开发板(STM32F103VET6)的迷宫小球(重力感应控制)游戏开发

    2013-03-03 这是研一上学期<实时嵌入式系统实验>课程的大作业,是利用野火板的资源,加上一个AHRS(Attitude and Heading Reference System,姿 ...

  9. Dom深入浅出

    Dom1级提供了一个Node接口,该接口将由Dom中所有节点类型(包括元素节点.文本节点.属性节点等12种)实现,而js是作为Node类型来实现的,js中的所有节点类型的继承自Node类型, 所以它们 ...

  10. ios键盘上添加辅助视图