zoukankan      html  css  js  c++  java
  • [CareerCup] 13.3 Virtual Functions 虚函数

    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
    ";
            return 0;
        }
    };
    
    class Triangle: public Shape {
    public:
        int circumference() {
            cout << "Circumference of Triangle Class
    ";
            return 3 * 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 0;
    }
  • 相关阅读:
    Blob
    MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法 (转)
    事务--存储过程
    JDBC-Mysql-编译预处理(占位符)
    socket
    GUI---深度复制
    串行化--深度复制
    RESTful理解
    django中文和时区的配置
    redis-server报错
  • 原文地址:https://www.cnblogs.com/grandyang/p/4922297.html
Copyright © 2011-2022 走看看