vptr的解析

发布于 2024-12-07 07:24:31 字数 266 浏览 0 评论 0原文

class base {
public:
    virtual void fn(){}
};

class der : public base {};

我知道编译器在类中提供了一个成员调用 VPTR,该成员在运行时由构造函数使用确切的 VTABLE 进行初始化。我有 2 个问题

1)哪个班级拥有 VPTR。或者所有班级都有单独的 VPTR。

2)当执行语句der d;时,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. I have 2 questions

1) Which class holds the VPTR. or all the class is having seperate VPTR.

2) When executing statement der d; how VPTR is being resolved at run time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-12-14 07:24:31

vtable 是为包含虚函数的类及其派生类创建的。这意味着在您的程序中 vtable 将为 base 创建class 和 der 类。这些 vtables 中的每一个都将包含虚拟函数 void fn() 的地址。现在请注意 der< /code> 类不包含 void 的定义fn(),因此它的vtable包含base类的void fn()函数的地址。因此,如果你进行调用就像d.fn();一样,base类的void fn()函数将被执行。

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(); the void fn() function of base class would get executed.

时光匆匆的小流年 2024-12-14 07:24:31

注意:虚拟表和虚拟指针是实现细节,尽管我知道的所有 C++ 编译器都使用它们,但它们不是标准所强制的,只有结果是。

要回答您的具体问题:每个具有虚拟方法(其自己的或继承的)的类的实例或具有(某处)虚拟继承关系的类的实例将至少需要一个虚拟指针。

可以有多个(当涉及虚拟继承或多继承时)。

您的示例中,单个虚拟指针就足够了。然而,将其视为的一部分是没有意义的。虚拟指针是实例(对象)的一部分,并且存在于类规则之外,因为这些规则适用于语言,并且虚拟指针是一种实现机制。

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.

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. 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.

酒与心事 2024-12-14 07:24:31

1) 哪个类持有 VPTR。或者所有班级都有单独的 VPTR。

如果是多态的(即包含虚拟类对象都有自己的vptr > 函数或具有虚拟继承。)在这种情况下,两个类都具有虚拟函数。

2)执行语句der d;时,VPTR在运行时如何解析?

您只是声明 der 的对象。但即使您调用函数,在这种情况下,对任何函数的调用都会在编译时解析。仅当使用指针/引用调用函数时,虚函数解析才会出现。

1) which class holds the VPTR. or all the class is having seperate VPTR.

Every class object has its own vptr if the class is polymorphic (i.e. contains virtual function or has virtual inheritance.) In this case both the classes has virtual function.

2) when executing statement der d; how VPTR is resolve at run time?

You are just declaring the object of 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文