我的内容可能写的比较潦草,如果需要笔记内容相对应的课程,可以私信我。
一,虚函数表指针(vptr)的位置
class A
{
public:
int m_i=0;
virtual void func()
{
}
};
int main()
{
A a;
cout << sizeof(a) << endl;
char* p1 = reinterpret_cast<char*>(&a);// 强制类型转换
char* p2 = reinterpret_cast<char*>(&(a.m_i));
if (p1 == p2)
cout << "虚函数表指针位于对象内存的结尾" << endl;
else
cout << "虚函数指针位于对象内存的开始位置" << endl;
}
![](https://i-blog.csdnimg.cn/blog_migrate/5f5061794a5db2e13a04aab26830fe9c.png)
结论: windows 上面的虚函数表指针都在对象内存最开始的位置;
linux在这一点上和windows一样;
二,继承关系作用下虚函数的手工调用
class Base//基类
{
public:
virtual void f() { cout << "Base::f()" << endl; }
virtual void g() { cout << "Base::g()" << endl; }
virtual void h() { cout << "Base::h()" << endl; }
};
//子类
class Derive :public Base
{
virtual void g() { cout << "Derive::g()" << endl; }
};
int main()
{
Derive* d = new Derive();//派生类指针;
long* p = (long*)d; // 指向对象的指针d转成long*;
long* vptr = (long*)(*p);//vptr指向虚函数表指针的地址;
typedef void(*Func)(void);//定义一个函数指针类型
Func f = (Func)vptr[0];
Func g = (Func)vptr[1];
Func h = (Func)vptr[2];
f();
g();
h();
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/87214dfe1822e8d76d65325fcce994a4.png)
结论:由于子类里面覆盖了基类的虚函数g(),所以子类指针调用的是Derive::g();
class Base//基类
{
public:
virtual void f() { cout << "Base::f()" << endl; }
virtual void g() { cout << "Base::g()" << endl; }
virtual void h() { cout << "Base::h()" << endl; }
};
//子类
class Derive :public Base
{
virtual void g() { cout << "Derive::g()" << endl; }
};
int main()
{
//Derive* d = new Derive();//派生类指针;
Base* d = new Base(); //基类指针;
long* p = (long*)d;
long* vptr = (long*)(*p);
typedef void(*Func)(void);
Func f = (Func)vptr[0];
Func g = (Func)vptr[1];
Func h = (Func)vptr[2];
f();
g();
h();
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/d3d1e11732aeceeea444f2346471751e.png)
结论:基类指针调用的是Base::g();
图解分析:
![](https://i-blog.csdnimg.cn/blog_migrate/e5b2c167a06f7b94edafb5c0172f34b3.png)
三:面向对象(OO)和基于对象(OB)
面向对象:object-orientedmodel:
C++通过基类的指针或引用指向派生类来支持多态,
基于对象:object-based:
不支持多态,执行速度快,因为函数调用的解析不需要运行时决定(没有多态),而是在编译期间执行完毕。内存空间更紧凑,因为没有虚函数指针和虚函数表这些概念;
总结:
OB的运行速度快,但是灵活性差;
C++既支持面向对象也支持基于对象;
四:多重继承下的虚函数表:
class Base1 // 基类1;
{
public:
virtual void f(){cout << "Base1::f()" << endl;}
virtual void g(){cout << "Base1::g()" << endl;}
};
class Base2//基类2;
{
public:
virtual void h(){cout << "Base2::h()" << endl;}
virtual void i(){cout << "Base2::i()" << endl;}
};
class Derived :public Base1,public Base2
{
public:
virtual void f(){cout << "Derived::f()" << endl;} // 覆盖父类1的f()虚函数;
virtual void i(){cout << "Derived::i()" << endl;} // 覆盖父类2的i()虚函数;
virtual void mh(){cout << "Derived::mh()" << endl;} // 自己的三个虚函数;
virtual void mi(){cout << "Derived::mi()" << endl;}
virtual void mj(){cout << "Derived::mj()" << endl;}
};
int main()
{
cout << sizeof(Base1) << endl;
cout << sizeof(Base2) << endl;
cout << sizeof(Derived) << endl;
Derived ins;
Base1& b1 = ins;
Base2& b2 = ins;
Derived& d = ins;
Base1 y;
Base2 x;
typedef void(*Func)(void);
long* p = (long*)(&ins);
long* vptr1 = (long*)(*p);//取第一个虚函数表指针;
long* pp = p + 1;//跳过4个字节;
long* vptr2 = (long*)(*pp);//取第二个虚函数表指针;
Func f1 = (Func)vptr1[0];// f1 = 0x00451550 {C++对象模型.exe!Derived::f(void)}
Func f2 = (Func)vptr1[1];// f2 = 0x0045153c {C++对象模型.exe!Base1::g(void)}
Func f3 = (Func)vptr1[2];// f3 = 0x00451519 {C++对象模型.exe!Derived::mh(void)}
Func f4 = (Func)vptr1[3];// f4 = 0x00451528 {C++对象模型.exe!Derived::mi(void)}
Func f5 = (Func)vptr1[4];// f5 = 0x00451541 {C++对象模型.exe!Derived::mj(void)}
Func f11 = (Func)vptr2[0];// f11 = 0x00451532 {C++对象模型.exe!Base2::h(void)}
Func f12 = (Func)vptr2[1];// f12 = 0x0045152d {C++对象模型.exe!Derived::i(void)}
return 0;
}
图解如下:
![](https://i-blog.csdnimg.cn/blog_migrate/374bdeda3db1e2074ce2988e14d3da10.png)
总结:
一个类有几个基类就有几个虚函数表;
这个类的对象就有几个虚函数指针;