[英]Resolving of vptr
class base {
public:
virtual void fn(){}
};
class der : public base {};
I know that compiler provides a member call VPTR in class which is initialised with the exact VTABLE at run time by constructor. 我知道编译器在类中提供了一个成员调用VPTR,该成员调用由构造函数在运行时使用确切的VTABLE初始化。 I have 2 questions
我有两个问题
1) Which class holds the VPTR. 1)哪个班级拥有VPTR。 or all the class is having seperate VPTR.
或所有班级都有单独的VPTR。
2) When executing statement der d;
2)执行语句
der d;
how VPTR is being resolved at run time? 在运行时如何解决VPTR?
vtable
is created for the class that contains virtual function and for the classes derived from it.It means in your program vtable
will be created for base
class and der
class.Each of these vtables
would contain the address of virtual function void fn()
.Now note that der
class doesn't contain the definition of void fn()
,hence its vtable
contains the address of base
class's void fn()
function.Thus if u make a call like d.fn();
vtable
是包含虚拟函数的类创建和用于从it.It装置在程序派生的类vtable
将用于创建base
类和der
这些的class.Each vtables
将包含虚拟函数的地址void fn()
现在注意der
类不包含void fn()
的定义,因此它的vtable
包含base
类的void fn()
函数的地址。因此,如果您像d.fn();
那样进行调用d.fn();
the void fn()
function of base
class would get executed. base
类的void fn()
函数将被执行。
Note: a virtual table and a virtual pointer are implementation details, though all the C++ compilers I know use them, they are not mandated by the Standard, only the results are. 注意:虚拟表和虚拟指针是实现细节,尽管我所知道的所有C ++编译器都使用它们,但标准并没有强制要求它们,只有结果是。
To answer your specific question: each instance of a class with virtual methods (either its own, or inherited ones) or a class with (somewhere) a virtual inheritance relationship will need at least one virtual-pointer. 要回答您的特定问题:具有虚拟方法的类的每个实例(自己的或继承的)或具有(某个地方)虚拟继承关系的类都将至少需要一个虚拟指针。
There can be several (when virtual inheritance or multi-inheritance are involved). 可以有多个(涉及虚拟继承或多重继承时)。
In your example, a single virtual pointer is sufficient. 在您的示例中,单个虚拟指针就足够了。 However it does not make sense to speak of it as being part of a
class
. 但是,将其称为
class
一部分是没有意义的。 The virtual pointer is part of the instance (object), and lives outsides the classes rules because those apply to the language, and the virtual pointer is an implementation mechanism. 虚拟指针是实例(对象)的一部分,并且不属于类规则,因为它们适用于语言,并且虚拟指针是一种实现机制。
1) which class holds the VPTR.
1)哪个班级拥有VPTR。 or all the class is having seperate VPTR.
或所有班级都有单独的VPTR。
Every class
object has its own vptr
if the class
is polymorphic (ie contains virtual
function or has virtual
inheritance.) In this case both the classes has virtual
function. 如果
class
是多态的(即包含virtual
函数或具有virtual
继承),则每个class
对象都有其自己的vptr
。在这种情况下,两个类都具有virtual
函数。
2) when executing statement der d;
2)执行语句der d时; how VPTR is resolve at run time?
在运行时如何解决VPTR?
You are just declaring the object of der
. 您只是在声明
der
的对象。 But even if you call a function then in this case the call to any function is resolved at compile time . 但是,即使您调用一个函数,在这种情况下,对任何函数的调用也会在编译时解决。 Virtual function resolution comes into picture only when the function is called with pointer/reference.
仅当使用指针/引用调用函数时,虚拟函数解析才会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.