众所周知,novtable
表示不为纯抽象类创建虚拟表。但是,当我运行代码打击时,出现了一些错误:
#include <iostream>
using namespace std;
struct A{
virtual void fun() = 0;
};
struct __declspec(novtable) B{
virtual void fun() = 0;
};
struct C{
void fun(){}
};
struct _declspec(novtable) D : public A {};
int main(){
cout<<sizeof(int*)<<endl; //4
cout<<sizeof(A)<<endl; //4
cout<<sizeof(B)<<endl; //4
cout<<sizeof(C)<<endl; //1
cout<<sizeof(D)<<endl; //4
return 0;
}
A
和B
的大小相同,是否意味着novtable
没有用?ps:用vs2019编译
最佳答案
类B
没有vtable。这并不意味着B
实例的对象中没有vtable指针。需要vtable指针,以便
例如,如果您创建BDerived的实例:
struct BDerived : public B {
void fun() {}
};
BDerived bd;
B* pb = &bd;
pb->fun();
pb
指向B
子对象,该子对象包含一个vtable指针,该指针指向BDerived
的vtable。调用pb->fun()
时,程序将查看pb
的vtable指针,将其跟随到BDerived
的vtable,然后在该vtable中查找以找到BDerived
的fun
实现。换句话说,编译器将代码转换为如下形式:
vtable A_vtable = {NULL};
struct A {
vtable *vtable_ptr;
};
// No vtable, but still a vtable pointer
struct B {
vtable *vtable_ptr;
};
void BDerived_fun() {};
vtable BDerived_vtable = {&BDerived_fun};
struct BDerived : public B {
};
BDerived bd; bd.vtable_ptr = &BDerived_vtable;
B* pb = &bd;
(pb->vtable_ptr.fun)(pb);
关于c++ - __declspec(novtable)是否没有用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58931606/