0
点赞
收藏
分享

微信扫一扫

CoFSM基于共现尺度空间的多模态遥感图像匹配方法--论文阅读记录

343d85639154 2024-04-24 阅读 8
#include <iostream>

using namespace std;

class Base
{
public:
    virtual void show() // 声明虚函数
    {
        cout << "Base" << endl;
    }
};

class Derived : public Base
{
public:
    void show() override // 覆盖虚函数
    {
        cout << "Derived" << endl;
    }
};

int main()
{
    Base *ptr = new Derived();
    ptr->show(); // 运行时解析 Q:为什么会输出 Derived ?
    delete ptr;
    return 0;
}

这里,调用ptr->show()时的具体步骤为:

  • ptr所指对象的内存开始处读取vptr(虚指针)

  • 使用vptr(虚指针)访问虚表。

  • 在虚表中查找show()函数对应的条目(因为Derived类覆盖了Base类show(),所以虚表里的指针指向Derived::show())。

  • 调用该地址对应的函数(即Derived::show())。

虚表使得C++中的多态成为可能,允许在运行时根据对象的实际类型调用适当的函数。

举报

相关推荐

0 条评论