虚函数

前言
C++中的虚函数的作用主要是实现了多态的机制。关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数。这种技术可以让父类的指针有“多种形态”这是一种泛型技术。所谓泛型技术,说白了就是试图使用不变的代码来实现可变的算法。比如:模板技术,RTTI技术,虚函数技术,要么是试图做到在编译时决议,要么试图做到运行时决议。

基础:虚函数的使用
虚函数的定义:
在函数的返回类型之前使用virtual
只在成员函数的声明中添加virtual, 在成员函数的实现中不要加virtual

虚函数的继承:
如果某个成员函数被声明为虚函数,那么它的子类【派生类】,以及子类的子类中,所继承的这个成员函数,也自动是虚函数。
如果在子类中重写这个虚函数,可以不用再写virtual, 但是仍建议写virtual, 更可读!

  进阶1:虚函数的原理-虚函数表

     单个类的虚函数表
        #include <iostream>
         using namespace std;

     class Father {
          public:
          virtual void func1() { cout << "Father::func1" << endl; }
          virtual void func2() { cout << "Father::func2" << endl; }
          virtual void func3() { cout << "Father::func3" << endl; }
          void func4() { cout << "非虚函数:Father::func4" << endl; }
         public:  //为了便于测试,特别该用public
          int x = 100;
          int y = 200;
         static int z;
         };

       typedef void (*func_t)(void);
       int Father::z = 1;
       int main(void) {
   Father father;

      // 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
         cout << "对象地址:" << (int*)&father << endl;

         int* vptr = (int*)*(int*)&father;
         cout << "虚函数表指针vptr:" << vptr << endl;

         cout << "调用第1个虚函数: ";
         ((func_t) * (vptr + 0))();

        cout << "调用第2个虚函数:";
          ((func_t) * (vptr + 1))();

       cout << "调用第3个虚函数: ";
           ((func_t) * (vptr + 2))();


       cout << "第1个数据成员的地址: " << endl;
       cout <<  &father.x << endl;
       cout << std::hex << (int)&father + 4 << endl;
       cout << "第1个数据成员的值:" << endl;
       cout << std::dec <<  father.x << endl;
       cout << *(int*)((int)&father + 4) << endl;

       cout << "第2个数据成员的地址: " << endl;
       cout << &father.y << endl;
       cout << std::hex << (int)&father + 8 << endl;
       cout << "第2个数据成员的值:" << endl;
       cout << std::dec << father.y << endl;
       cout << *(int*)((int)&father + 8) << endl;

       cout << "sizeof(father)==" << sizeof(father) << endl;

    Father father2;
     cout << "father的虚函数表:";
     cout << *(int*)(*(int*)&father) << endl;
     cout << "father2的虚函数表:";
     cout << *(int*)(*(int*)&father2) << endl;

      system("pause");
     return 0;
      }

执行效果:

VS的对象内存分布分析:
项目的命令行配置中添加: /d1 reportSingleClassLayoutFather 


手绘内存分布:

       对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。
       然后再存储非静态数据成员。

      对象的非虚函数,保存在类的代码中!
      对象的内存,只存储虚函数表和数据成员
   (类的静态数据成员,保存在数据区中,和对象是分开存储的)

     添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目
    多个对象,共享同一个虚函数表
posted @ 2020-01-02 17:30  蔺雨轩  阅读(258)  评论(0)    收藏  举报