虚函数指针和虚函数表
Posted P=NP
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虚函数指针和虚函数表相关的知识,希望对你有一定的参考价值。
虚函数指针和虚函数表
虚函数表的定义
多态是由虚函数实现的,而虚函数主要是通过虚函数表(V-Table)来实现的。
如果一个类中包含虚函数(virtual修饰的函数),那么这个类就会包含一张虚函数表(vftbl),虚函数表存储的每一项是一个虚函数的地址。在一个对象的内存布局中,指向这张虚函数表的指针(vfptr)位于最前端。如下图:
一般继承(无虚函数覆盖)
对于如下UML的类:在普通继承情况,派生类没有重写基类的虚函数:
体现在代码上为:
#include <iostream>
using namespace std;
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
{
public:
virtual void f1()
{
cout << "Derive:f1()" << endl;
}
virtual void g1()
{
cout << "Derive:g1()" << endl;
}
virtual void h1()
{
cout << "Derive:h1()" << endl;
}
};
int main() {
Base base;
Derive derive;
}
使用lldb查看虚函数指针和虚函数表内容:
根据结果,可以得知其虚函数表如下图:
一般继承(有虚函数覆盖)
对于如下UML的类:在普通继承情况,派生类重写了基类的虚函数( f() ):
代码为:
#include <iostream>
using namespace std;
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
{
public:
virtual void f()
{
cout << "Derive:f()" << endl;
}
virtual void g1()
{
cout << "Derive:g1()" << endl;
}
virtual void h1()
{
cout << "Derive:h1()" << endl;
}
};
int main() {
Base base;
Derive derive;
}
结果为:
绘制成图,为:
多重继承(无虚函数覆盖)
对于如下UML的类:在多重继承情况,派生类没有重写了基类的虚函数:
代码为:
#include <iostream>
using namespace std;
class Base1
{
public:
virtual void f()
{
cout << "Base1:f()" << endl;
}
virtual void g()
{
cout << "Base1:g()" << endl;
}
virtual void h()
{
cout << "Base1:h()" << endl;
}
};
class Base2
{
public:
virtual void f()
{
cout << "Base2:f()" << endl;
}
virtual void g()
{
cout << "Base2:g()" << endl;
}
virtual void h()
{
cout << "Base2:h()" << endl;
}
};
class Base3
{
public:
virtual void f()
{
cout << "Base3:f()" << endl;
}
virtual void g()
{
cout << "Base3:g()" << endl;
}
virtual void h()
{
cout << "Base3:h()" << endl;
}
};
class Derive : public Base1, public Base2, public Base3
{
public:
virtual void f1()
{
cout << "Derive:f1()" << endl;
}
virtual void g1()
{
cout << "Derive:g1()" << endl;
}
};
int main() {
Derive derive;
}
结果为:
绘制成图,为:
多重继承(有虚函数覆盖)
对于如下UML的类:在多重继承情况,派生类没有重写了基类的虚函数:
代码为:
#include <iostream>
using namespace std;
class Base1
{
public:
virtual void f()
{
cout << "Base1:f()" << endl;
}
virtual void g()
{
cout << "Base1:g()" << endl;
}
virtual void h()
{
cout << "Base1:h()" << endl;
}
};
class Base2
{
public:
virtual void f()
{
cout << "Base2:f()" << endl;
}
virtual void g()
{
cout << "Base2:g()" << endl;
}
virtual void h()
{
cout << "Base2:h()" << endl;
}
};
class Base3
{
public:
virtual void f()
{
cout << "Base3:f()" << endl;
}
virtual void g()
{
cout << "Base3:g()" << endl;
}
virtual void h()
{
cout << "Base3:h()" << endl;
}
};
class Derive : public Base1, public Base2, public Base3
{
public:
virtual void f()
{
cout << "Derive:f()" << endl;
}
virtual void g1()
{
cout << "Derive:g1()" << endl;
}
};
int main() {
Derive derive;
}
结果为:
绘制成图,为:
以上是关于虚函数指针和虚函数表的主要内容,如果未能解决你的问题,请参考以下文章