多态-虚函数表

简介: 多态-虚函数表

VS的对象内存分析:

/d reportSingleClassLayout+类名

使用方法:项目 -- 属性 -- C/C++ -- 命令行--其他选型(D) 添加命令.如图所示:

Father类:

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:
  int x = 200;
  int y = 300;
  static int z;
};
int Father::z = 0;

Father的内存分析:

/d reportSingleClassLayoutFather

 

Son类: public Father

class Son:public Father
{
public:
  void Func1() { cout << "Son::Func1" << endl; }
  virtual void Func5() { cout << "Son::Func5" << endl; }
private:
  int m = 400;
  int n = 500;
};

Son类的内存分析:

/d reportSingleClassLayoutSon

子类的虚函数表构建过程:

(1)直接复制父类的虚函数表

(2)如果子类重写了父类的某个虚函数,那么就在这个虚函数表中进行相应的替换

(3)如果子类中添加了新的虚函数,就把这个虚函数添加到虚函数的表中(在尾部添加)

 

注意:所有子类共用同一张虚函数表

main函数:

int main(void) {
  Son son;
  Father* father = &son;
  cout << "----Father* father = &son----" << endl;
  son.Func1();
  son.Func2();
  son.Func3();
  son.Func4();
  son.Func5();
  Father* son2 = new Son();
  cout << "----Father* son2 = new Son()----" << endl;
  son2->Func1();
  son2->Func2();
  son2->Func3();
  son2->Func4();
  //son2->Func5();//错误使用
  return 0;
}

结果:

全部代码(测试):

#include<iostream>
using namespace std;
//vs的对象内存分析:/d1 reportSingleClassLayout+类名
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:
  int x = 200;
  int y = 300;
  static int z;
};
int Father::z = 0;
class Son:public Father
{
public:
  void Func1() { cout << "Son::Func1" << endl; }
  virtual void Func5() { cout << "Son::Func5" << endl; }
private:
  int m = 400;
  int n = 500;
};
typedef void(*func)(void);
void test(void) {
  Father father;
  cout << "sizeof(father): " << sizeof(father) << endl;
  cout << "(int*)&father: " << (int*)&father << endl;
  cout << "&father: " << &father << endl;
  int* vptr = (int*)*(int*)(&father);
  cout << "调用虚函数Func1:" << endl;
  ((func) * (vptr + 0))();
  cout << "调用虚函数Func2:" << endl;
  ((func) * (vptr + 1))();
  cout << "调用虚函数Func3:" << endl;
  ((func) * (vptr + 2))();
  cout << "father.x地址: " << endl;
  cout << &father.x << endl;
  cout << (int*)((int)&father + 4) << endl;
  cout << "father.x:" << endl;
  cout << father.x << endl;
  cout << *(int*)((int)&father + 4) << endl;
  cout << "father.y地址: " << endl;
  cout << &father.y << endl;
  cout << (int*)((int)&father + 8) << endl;
  cout << "father.y:" << endl;
  cout << father.y << endl;
  cout << *(int*)((int)&father + 8) << endl;
}
int main(void) {
  test();
  Son son;
  Father* father = &son;
  cout << "----Father* father = &son----" << endl;
  son.Func1();
  son.Func2();
  son.Func3();
  son.Func4();
  son.Func5();
  Father* son2 = new Son();
  cout << "----Father* son2 = new Son()----" << endl;
  son2->Func1();
  son2->Func2();
  son2->Func3();
  son2->Func4();
  //son2->Func5();//错误使用
  return 0;
}
目录
相关文章
|
7月前
|
C++
9. C++虚函数与多态
9. C++虚函数与多态
62 0
|
C++
38.【C++ 虚函数 纯虚函数 虚基类 (最全详解)】(一)
38.【C++ 虚函数 纯虚函数 虚基类 (最全详解)】
57 0
|
C++
38.【C++ 虚函数 纯虚函数 虚基类 (最全详解)】(二)
38.【C++ 虚函数 纯虚函数 虚基类 (最全详解)】
122 0
|
7月前
|
C++ 编译器 存储
|
编译器 C++
C++中的多态和虚函数
#include <iostream> using namespace std; //基类People class People{ public: People(char *name, int age); void display(); protected: char *m_name; int m_age; }; People::People(char *name, int age): m_name(name), m_age(age){} void Peopl
C++中的多态和虚函数
|
存储 编译器 C++
<c++>虚函数与多态 | 虚函数与纯虚函数 | 多态的实现原理 | 虚析构函数
<c++>虚函数与多态 | 虚函数与纯虚函数 | 多态的实现原理 | 虚析构函数
144 0
|
7月前
|
存储 编译器 C++
C++多态与虚函数
C++多态与虚函数
76 0
|
7月前
|
NoSQL 编译器 程序员
『 C++类与对象 』虚函数与多态
『 C++类与对象 』虚函数与多态
|
7月前
|
存储 NoSQL 编译器
『 C++类与对象 』多态之单继承与多继承的虚函数表
『 C++类与对象 』多态之单继承与多继承的虚函数表
多重继承的虚函数表
多重继承的虚函数表
46 0