zoukankan      html  css  js  c++  java
  • C++ Daily 《5》----虚函数表的共享问题

    版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u012653791/article/details/25537161

    问题:

    包括一个以上虚函数的 class B, 它所定义的 对象是否共用一个虚函数表?


    分析: 因为含有虚函数,因此对象内存包括了一个指向虚函数表的指针,可是这个指针指向的是同一个虚函数表吗?

    实验例如以下:

    class A
    {
    public:
      virtual void print() { cout << "print A:" << endl;}
    
    private:
      int a;
    };
    
    
    A a[2];
    cout << "vptr of a[0] " << *(int*)&a[0] << endl;
    cout << "vptr of a[1] " << *(int*)&a[1] << endl;
    

    结论:

    结果表面,同一个类的全部对象,都共享同一个虚函数表。


    派生问题:

    派生类 和 基类是否共享同一个虚函数表呢?


  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/9929964.html
Copyright © 2011-2022 走看看