java – 虚拟表/调度表
作者:互联网
根据我所知的CPP,每个班级都有自己的vtable.
但是this维基百科链接提到:
An object’s dispatch table will
contain the addresses of the object’s
dynamically bound methods. Method
calls are performed by fetching the
method’s address from the object’s
dispatch table. The dispatch table is
the same for all objects belonging to
the same class, and is therefore
typically shared between them.
有人可以请一些亮点.
谢谢 !
解决方法:
通过示例有时更容易理解:
class PureVirtual {
public:
virtual void methodA() = 0;
virtual void methodB() = 0;
};
class Base : public PureVirtual {
public:
virtual void methodA();
void methodC();
private:
int x;
};
class Derived : public Base {
public:
virtual void methodB();
private:
int y;
};
因此,给定Derived类型的对象,它可能看起来像:
------------
Known offset for vtable | 0xblah | -------> [Vtable for type "Derived"]
------------
Known offset for x | 3 |
------------
Known offset for y | 2 |
------------
使用Vtable类型“Derived”看起来像:
------------
Known offset for "methodA" | 0xblah1 | ------> methodA from Base
-------------
Known offset for "methodB" | 0xblah2 | ------> methodB from Derived
-------------
请注意,由于“methodC”不是虚拟的,因此它根本不在vtable中.另请注意,Derived类的所有实例都将具有指向相同的共享vtable对象的vtable指针(因为它们具有相同的类型).
虽然C和Java的实现略有不同,但这些想法并不相容.从概念的角度来看,关键区别在于Java方法是“虚拟的”,除非声明为“最终”.在C中,必须明确给出关键字“virtual”,以使函数在vtable中.任何不在vtable中的东西都将使用编译时类型而不是对象的运行时类型来调度.
标签:java,c,oop,virtual-functions 来源: https://codeday.me/bug/20190926/1818070.html