首页 > 编程语言 >C++多继承下,派生类对象有几张虚函数表?

C++多继承下,派生类对象有几张虚函数表?

时间:2022-10-29 19:38:10浏览次数:46  
标签:几张 cout 基类 Derived C++ virtual 派生类 重写


#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

//基类
class Base1
{
public:
Base1() : x(1) {}
virtual void play() { cout << "Base1::play basketball" << endl; }
virtual void dance() { cout << "Base1::dance dance" << endl; }
private:
int x;
};

//基类
class Base2
{
public:
Base2() : y(2) {}
virtual void print() { cout << "Base2::print hello world" << endl; }
private:
int y;
};

class Base3{
public:
Base3() : z(3) {}
virtual void sing() { cout << "Base3::sing song" << endl; }

private:
int z;
};

//派生类多继承
class Derived : public Base1, public Base2, public Base3{
public:
Derived() : w(4) {}
virtual void play() { cout << "Derived::play basketball" << endl; }
virtual void print() { cout << "Derived::print hello world" << endl; }
virtual void sing() { cout << "Derived::sing song" << endl; }
private:
int w;
};

int main(){
Base1* p1 = new Derived();
Base2* p2 = new Derived();
Base3* p3 = new Derived();

p1->play();
p2->print();
p3->sing();
p1->dance();

return 0;
}
Derived::play basketball                // 派生类重写的虚函数,vftable记录的是重写方法的地址
Derived::print hello world // 派生类重写的虚函数,vftable记录的是重写方法的地址
Derived::sing song // 派生类重写的虚函数,vftable记录的是重写方法的地址
Base1::dance dance // 派生类没有重写dance,vftable记录的是基类方法的地址

查看类空间布局:

cl main.cpp /d1reportSingleClassLayoutDerived

C++多继承下,派生类对象有几张虚函数表?_算法

派生类对象有三个vfptr,分别对应于三个基类

这些东西在编译阶段就生成指令了,哪个类型的基类指针,访问的就是对应的vfptr,找对应的vftable,取虚函数地址

单继承中,基类有虚函数时,派生类会继承其vfptr,如果派生类有重写虚函数,vftable中就会更新重写方法的地址,如果没有重写虚函数,vftable记录的依然是基类中函数的地址


标签:几张,cout,基类,Derived,C++,virtual,派生类,重写
From: https://blog.51cto.com/BugMaker/5806449

相关文章

  • C/C++ 交换机管理命令实战
    #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<conio.h>usingnamespacestd;structport{charname[16];i......
  • C++ primer 7.2 7.3笔记
    7.2访问控制与封装访问说明符:public,privateclass和struct的区别:默认访问权限不一样,class默认所有成员是private,struct默认所有成员是public。7.2.1友元类可以允许......
  • C++20 实现字符串类型的转换操作
    文章目录​​1、C语言库​​​​1.1printf/scanf(Clibrary)​​​​1.2floor/ceil/round(Clibrary)​​​​1.3itoa/atoi(Clibrary)​​​​1.4sprintf(Clibrary)......
  • c++17 注解标签 attributes & c++ 枚举值
    c++标注c++17后逐渐完善注解标签语法:[[attribute]]types/functions/enums/etc 告诉编译器没有返回值[[noreturn]]常用于系统函数设计,如std::abort()std::exit()......
  • C++创建桌面应用程序:处理对话框DialogBox
    VS2019新建C++桌面向导://Project1.cpp:定义应用程序的入口点。//#include"framework.h"#include"Project1.h"INT_PTRDlgproc(HWNDhwndDlg,UINTuMsg,WPARAMwParam......
  • C/C++ struct结构体的三种使用方法
    #include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<conio.h>usingnamespacestd;structhero{charname[16];ch......
  • 周六900C++班级2022-10-29 广搜
    7588:农夫抓牛农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:1、从X移动到X-1或X+......
  • 记录第一次使用c++和汇编联合编译
    32位从Ida把目标函数扣出来toUapperprocneararg_0=dwordptr4pushesimovesi,[......
  • C++ primer笔记 7.1 定义抽象数据类型
    7.1定义抽象数据类型structSales_data{std::stringbookNo;unsignedunits_sold=0;doublerevenue=0.0;std::stringisbn()const{returnboo......
  • Homework 3 : C++ class inheritance Answer
    Homework3:C++classinheritanceAnswerHomework3:C++classinheritanceInstructor:ZhiyaoLiangCIS111&EIE1112021Spring源码传送门传送门:https://pan.ba......