gpt4 book ai didi

c++ - vptr 中没有列出虚函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:45 27 4
gpt4 key购买 nike

vptr 索引应该显示所有虚函数,但在我的例子中,3 个虚函数中只显示了 2 个。

我在下面提供完整的代码和屏幕截图:-

类头.h

#include <iostream>
using namespace std;

// Employee Class

class Employee
{
public :
int salary ;
Employee(){cout << "Inside CTOR" << endl;}

virtual ~Employee() {cout << "Inside DTOR" << endl;}

virtual void pay(){cout << "Employee" << endl;}
};

// Manager Class

class Manager : public Employee
{
public :

virtual void pay(){cout<< "Manager pay" << endl;}
virtual void Rank(){cout << "Manager Rank" << endl;}
};

// JuniorManager Class
class JuniorManager : public Manager
{
public :

virtual void pay(){cout<< "JuniorManager pay" << endl;}
virtual void Rank(){cout << "JuniorManager Rank" << endl;}
};

main.cpp

#include "ClassHeader.h"

void main()
{
Manager *p = new Manager();

p->pay();
p->Rank();

p = new JuniorManager();
p->Rank();

Employee *pE = dynamic_cast<Employee*>(p);
pE->pay();

}

Manager 类有两个虚函数,pay 和 Rank,但只有 pay 出现在 vptr 中。

谁能告诉我,为什么 Rank 没有出现,即使它是虚函数。

我在 Windows 7 64 位上使用 Visual Studio 2008 和最新更新。

enter image description here

JuniorManager 调试器屏幕截图

它也不显示虚函数。请看下图。

enter image description here

最佳答案

如果您将类作为 Employee 检查,因为该类没有 Rank(),它不会在 vtable 中显示 Rank()。您的屏幕截图显示了 Employee 类的内容。

"Yup, the debugger doesn't have sufficient type information to tell how long the array is. So it only displays the first element unless overridden."

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/23d4e48e-520e-45b4-8c2f-65c11946d75d

关于c++ - vptr 中没有列出虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9166557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com