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. Erlang入门(二)—并发编程

    Erlang中的process——进程是轻量级的,并且进程间无共享.查了很多资料,似乎没人说清楚轻量级进程算是什么概念,继续查找中...闲话不提,进入并发编程的世界.本文算是学习笔记,也可以说是< ...

  2. (一)学习MVC之制作验证码

    制作验证码的方法在@洞庭夕照 看到的,原文链接:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 现自己利用该方法制作一个简单的 ...

  3. java中的快捷键

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  4. 可用于Hadoop下的ETL工具——Kettle

    看大家分享了好多hadoop相关的一些内容,我为大家介绍一款ETL工具——Kettle.    Kettle是pentaho公司开源的一款ETL工具,跟hadoop一样,也是java实现,其目的就是做 ...

  5. UMDF

    看了一周的UMDF,今天就将这些天的心得记下来. 一开始,老大推荐看<竹林蹊径:深入浅出Windows驱动开发完美补全版.张佩马勇董鉴源.扫描版>.这本书看了前三章,这本书讲的太细,作者又 ...

  6. 编程精粹:编写高质量的C语言代码———笔记一

    第一章 假想的编译程序 要记得对空语句进行处理,最好使用NULL使其明显可见 char * strcpy(char* pchTo, char* pchFrom) { char* pchStart = ...

  7. 设备扩展(DEVICE_EXTENSION)

    原文链接:http://blog.csdn.net/hazy/article/details/481705 WDM中的结构   ---设备扩展 设备扩展(DEVICE_EXTENSION)是与设备对象 ...

  8. C/C++编译预处理命令详解【转】

    1.       预处理程序  按照ANSI标准的定义,预处理程序应该处理以下指令: #if #ifdef #ifndef #else #elif #endif #define #undef #lin ...

  9. Hadoop概念学习系列之hadoop、spark常备查询网址(二十九)

    http://archive.apache.org/dist

  10. 【转】可执行程序包括BSS段、数据段、代码段

    可执行程序包括BSS段.数据段.代码段(也称文本段). 一.BSS BSS(Block Started by Symbol)通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域.特点是:可 ...