1.动态联编,本质是在运行时多态的表现。
2.其本质是因为维护了一张虚函数表,虚函数表以链表的形式存在。每个结点存储了对象指针的地址,通过一个指针进行遍历索引。
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
class c
{
public:
c();
~c();
virtual string tostring()
{
return "c";
}
private:
};
class b: public c
{
public:
b();
~b();
string tostring()
{
return "b";
}
private:
};
class a : public c
{
public:
a();
~a();
string tostring()
{
return "a";
}
private:
};
c::c()
{
}
c::~c()
{
}
b::b()
{
}
b::~b()
{
}
a::a()
{
}
a::~a()
{
}
void display(c *x)
{
cout << x->tostring() << endl;
}
int main()
{
a p1 = a();
b p2 = b();
c p3 = c();
display(&p1);
display(&p2);
display(&p3);
return 0;
}
————————————————
版权声明:本文为CSDN博主「༄yi笑奈何」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40179458/article/details/115438491