码迷,mamicode.com
首页 > 其他好文 > 详细

虚函数

时间:2020-01-02 18:36:49      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:虚函数   sizeof   typedef   virtual   技术   def   一个   stream   个数   

基础:虚函数的使用
虚函数的定义:
在函数的返回类型之前使用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 

技术图片
手绘内存分布:
技术图片

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

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

     添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目
    多个对象,共享同一个虚函数表

虚函数

标签:虚函数   sizeof   typedef   virtual   技术   def   一个   stream   个数   

原文地址:https://www.cnblogs.com/linyuxuan/p/12134185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!