zoukankan      html  css  js  c++  java
  • 单继承时虚函数表解析

    1.派生类先继承基类的虚函数表,如果派生类重新定义基类的虚函数,则会覆盖相应的表项,如果派生类定义了新的虚函数,则按照声明的顺序扩充虚函数表。
    2. 注意:对于同时存在于基类和派生类的虚函数,派生类虚函数表的顺序是按照基类虚函数的声明顺序排列的,与派生类声明顺序无关(微信面试题)。

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    
    class Base {
    public:
        virtual void f() { cout << "Base::f" << endl; }
        virtual void g() { cout << "Base::g" << endl; }
        virtual void h() { cout << "Base::h" << endl; }
    
    };
    
    class Derived : public Base
    {
        void g() { cout << "Derived::g" << endl; }
        void h() { cout << "Derived::h" << endl; }
        void f() { cout << "Derived::f" << endl; }
        virtual void i() { cout << "Derived::i" << endl;}
    };
    
    int main()
    {
        Base base;
        typedef void(*pFun)();
        pFun pfun;
        cout << "Based---------------------------------------" << endl;
        for (int i = 0; i < 3; i++)
        {
            pfun = (pFun)*((int *)*(int *)(&base) + i);
            pfun();
        }
        cout << "Derived-------------------------------------" << endl;
        Derived derived;
        for (int i = 0; i < 4; i++)
        {
            pfun = (pFun)*((int *)*(int *)(&derived) + i);
            pfun();
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    c# 创建多线程
    使用opencvsharp通过mvvm在image中显示图片
    c# 创建文件/文件夹对话框
    wpf MVVM框架基础
    wpf DataBinding
    layui自动点击下拉列表的一项并选中
    LayUI默认样式调整
    mysql取某个组的前n条数据
    Kali3.0系统切换中文
    JS触发某元素周围元素的样式改变
  • 原文地址:https://www.cnblogs.com/lakeone/p/5927339.html
Copyright © 2011-2022 走看看