纯虚函数和虚拟表 [英] Pure virtual functions and virtual table
问题描述
大家好,
我知道一个类的虚拟表存储了虚函数的地址。我的问题是,如果虚拟表中存储了什么值,该类具有纯虚函数,0表示纯虚函数
我在线搜索但我无法找到好的答案
提前谢谢,
纯虚函数是一个虚函数,其声明以= 0结尾:
class 基数{
// < span class =code-comment> ...
virtual void f()= 0 ;
// ...
引用:如果类具有纯虚函数,将在虚拟表中存储哪些值
纯函数和非纯函数之间存在细微差别,看看:
Quote:
对于非纯虚函数,vtable中的每个条目都会引用到最终覆盖者或者如果需要的话适应这个指针的thunk。
在纯虚函数的情况下,vtable中的条目通常包含一个指向泛型函数的指针,该函数通过一些合理的消息来抱怨和中止程序(纯在此上下文中调用的虚函数或类似的错误消息)。
Quote:0表示纯虚函数
0表示这是一个纯虚函数!纯虚函数隐式地使它为抽象定义的类(与Java中不同的是,您有一个关键字来显式声明类抽象)。抽象类无法实例化。派生类需要覆盖/实现所有继承的纯虚函数。如果他们不这样做,他们也会变得抽象。
[已编辑]
引用:1)编译器如何知道vtable是否具有纯虚函数条目或虚函数。
2)我们如何调用基类纯虚函数。我试图从派生类进行静态调用我有一个链接器错误? 。如果可能,请提供示例代码
1):编译器不需要知道!如果纯虚函数是NULL而不是地址,则会引发异常。在常规虚函数的情况下,有一个已实现函数的地址,它将被调用。
到2):
class A
{
public :
virtual void foo()= 0 跨度>;
};
class B: public 虚拟 A
{
public :
void foo(){cout<< foo(); }
};
int main()
{
A * obj = new B();
obj-> foo();
return 0 ;
}
详情请见此处虚拟功能和vtable是如何实现的?在堆栈溢出 [ ^ ]。
Hi all,
I know that virtual table of a class stores the addresses of virtual functions .My question is, what values will be stored in virtual table if the class has pure virtual function and what does 0 indicates in pure virtual functions
I searched online but I couldn't able to find good answers
Thanks in advance,解决方案A pure virtual function is a virtual function whose declaration ends in =0:
class Base { // ... virtual void f() = 0; // ...
Quote:what values will be stored in virtual table if the class has pure virtual function
There is a subtle difference between pure- and non-pure-functions, take a look:
Quote:
In the case of non-pure virtual functions, each entry in the vtable will refer to the final-overrider or a thunk that adapts the this pointer if needed.
In the case of a pure-virtual function, the entry in the vtable usually contains a pointer to a generic function that complains and aborts the program with some sensible message (pure virtual function called within this context or similar error message).
Quote:what does 0 indicates in pure virtual functions
A 0 indicates that this is a pure virtual function! A pure virtual function implicitly makes the class it is defined for abstract (unlike in Java where you have a keyword to explicitly declare the class abstract). Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract.
[EDITED]
Quote:1) How does the compiler knows whether vtable has the pure virtual function entry or virtual function .
2) How we can make the call to base class pure virtual function. I tried to make a static call from derived class i got a linker error? .If possible can you please provide sample code
To 1): The compiler don't need to know! In case of pure-virtual-function is a NULL instead of address, it raises the exception. In case of a regular virtual function there is an address of the implemented function, it will be called.
To 2):
class A { public: virtual void foo()=0; }; class B : public virtual A { public: void foo() { cout<<"foo()"; } }; int main() { A* obj = new B(); obj->foo(); return 0; }
See here for some info "How are virtual functions and vtable implemented?" at Stack Overflow[^].
这篇关于纯虚函数和虚拟表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!