13.3 How do virtual functions work in C++?

这道题问我们虚函数在C++中的工作原理。虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table。当类中定义了虚函数时,一个虚表格就建立了用来保存该类的虚函数的地址。此时编译器Compiler也会在该类中增加一个虚指针vptr(Virtual Pointer),用来指向虚表格。当一个虚函数在派生类中没有被重写时,派生类中的虚表格中仍然存的是基类的虚函数的地址。当虚函数被调用时,就要到虚表格中取找函数地址。C++中的动态绑定机制主要就是通过虚表格来实现的。

当我们将基类的指针指向一个派生类的实体时,虚指针vptr就指向派生类的虚表格,这样就保证了派生类中的虚函数能被调用,参见如下代码:

class Shape {
public:
int edge_len;
virtual int circumference() {
cout << "Circumference of Base Class\n";
return ;
}
}; class Triangle: public Shape {
public:
int circumference() {
cout << "Circumference of Triangle Class\n";
return * edge_len;
}
}; int main() { Shape *x = new Shape();
x->circumference(); // "Circumference of Base Class"
Shape *y = new Triangle();
y->circumference(); // "Circumference of Triangle Class" return ;
}

[CareerCup] 13.3 Virtual Functions 虚函数的更多相关文章

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

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

  2. virtual之虚函数,虚继承

    当类中包含虚函数时,则该类每个对象中在内存分配中除去数据外还包含了一个虚函数表指针(vfptr),指向虚函数表(vftable),虚函数表中存放了该类包含的虚函数的地址. 当子类通过虚继承的方式从父类 ...

  3. C++语言基础(13)-抽象类和纯虚函数

    一.基本语法 在C++中,可以将虚函数声明为纯虚函数,语法格式为: ; 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上=0,表明此函数为纯虚函数. 最后的=0并不表示函数返回值为0,它只起形 ...

  4. C++中virtual(虚函数)的用法

    在面向对象的C++语言中,虚函数(virtual function)是一个非常重要的概念. 什么是虚函数: 虚函数是指一个类中你希望重载的成员函数 ,当你用一个  基类指针或引用   指向一个继承类对 ...

  5. c++ virtual function 虚函数面试题

    下面的代码输出什么? #include<iostream> using namespace std; class A { public: virtual void foo() { cout ...

  6. 【足迹C++primer】52、,转换和继承虚函数

    转换和继承,虚函数 Understanding conversions between base and derived classes is essential to understanding h ...

  7. C++ virtual虚函数

    #include<iostream> using namespace std; class Base{ public: void m() { cout << "it' ...

  8. C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址

    C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址 讲解之前,了解下什么是虚函数,什么是虚表指针,了解下语法,(也算复习了) 开发知识为了不码字了,找了一篇介绍比较好的,这里我扣过来了,当 ...

  9. C++: 多态 虚函数

    一.多态: 1.多态是什么:具有不同功能的函数可以用同一个函数名 2.静态多态:程序编译时决定,通过函数重载实现. 3.动态多态:程序运行时决定,通过虚函数实现. 二.虚函数: 1.引入目的:可以通过 ...

随机推荐

  1. LeetCode 8 String to Integer (string转int)

    题目来源:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an ...

  2. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. iOS开发者必备的10款工具

    当前iOS和Android两大移动操作系统“二足鼎立”,几乎覆盖了市面上大部分的智能手机.相比Android,iOS开发适配更简单,且随着各种实用工具和Swift语言的出现,iOS开发门槛地降低,开发 ...

  4. [转载] 数据测试常用的Data Profiling方法

    现在对数据质量的要求越来越高,面对一个动辄上亿条数据的报表如何快速对它的数据质量做出分析呢?给大家分享下我们测试时用到的Data Profiling方法. Data Profiling,可以大概翻译“ ...

  5. TCP面向连接网络编程

    一 TCP&UDP协议 TCP,Tranfer Control Protocol,是一种面向连接的保证可靠传输的协议.通过TCP协议传输,得到的是一个顺序的无差错的数据流.发送方和接收方的成对 ...

  6. golang中os/exec包用法

    exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. 1.func LookPath(file string) ( ...

  7. [转][原]openstack-kilo--issue(六)kilo版openstack的dashboard在session超时后重新登录报错解决办法

    http://blog.csdn.net/wylfengyujiancheng/article/details/50523373?locationNum=1&fps=1 1.现象描述: kil ...

  8. cocos2d-x之利用富文本控件遍历xml

    1. #ifndef SuperRichText_hpp #define SuperRichText_hpp #include <stdio.h> #include "cocos ...

  9. ElasticSearch Filter Aggregations

    类似于sql语句中where子句的作用 { "query": { "match_all": {} }, "aggs": { "ag ...

  10. mac下 ssh免密码登陆设置

    由于mac os 是基于unix的操作系统终端和linux非常类似,所以不用借助类似于windows下的putty 和CRT工具即可远程登陆linux服务器,只需简单地3步即可免密码ssh远程. 1 ...