English 中文(简体)
虚拟桌子(C++)何时被设定为物体?
原标题:When exactly does the virtual table pointer (in C++) gets set for an object?

我知道,对于任何具有虚拟功能的班级或来自具有虚拟功能的班级的班级,编篡人有两件事。 首先,它为这一类创造了一个虚拟表格,其次,它把虚拟点(变压器)放在物体的基数部分。 在开学期间,这名验收者被分配,在物体被当场时,便开始指出正确的说法。

My question is that where exactly in the instantiation process does this vptr gets set? Does this assignment of vptr happens inside the constructor of the object of before/after the constructor?

最佳回答

这完全取决于执行。

For Most compilers,

汇编者在每个构造者成员初始化人名单内初步确定this->__vptr

这样做的思想是,让每个物体的识别器在舱面上点击,汇编者为此制定隐蔽的代码,并将其添加到建筑商法典中。 类似:

Base::Base(...arbitrary params...)
   : __vptr(&Base::__vtable[0])  ← supplied by the compiler, hidden from the programmer
 {
   
 }

<>

问题回答

表格的点子在各层次的每个构造者进入时更新,然后又在每一名下士进入时更新。 接收器将开始点到基类,然后随着不同层次的启动而更新。

虽然你们会从许多不同的人那里读到,这是执行的定义,因为这是所有的表格选择,但事实是,所有汇编者都使用表格,一旦你选择了一种可变的方法,标准的确规定,操作时间标的类型是正在执行的施工者/司机的类型,而这反过来又意味着,无论动态发送机制是什么,都必须随着建筑/建筑链的渗透而加以调整。

Consider the following code snippet:

#include <iostream>

struct base;
void callback( base const & b );
struct base {
   base() { callback( *this ); }
   ~base() { callback( *this ); }
   virtual void f() const { std::cout << "base" << std::endl; }
};
struct derived : base {
   derived() { callback( *this ); }
   ~derived() { callback( *this ); }
   virtual void f() const { std::cout << "derived" << std::endl; }
};
void callback( base const & b ) {
   b.f();
}
int main() {
   derived d;
}

本方案的产出是<条码><<> >/代码>,derived,derived,base,但<条码>查询<><<<>>条/代码>与功能上的所有四条电话相同。 唯一可以执行的方法是,将物体的吸收器作为建筑/销毁的进展加以更新。

在施工人身上,如果使用<代码>vptr,<代码>vptr,则可以称为虚拟功能。

请注意,在轨迹上的虚拟功能是 >: 构造者等级,而不是可能由更衍生的等级推翻的类型。

#include <iostream>

struct A
{
    A() { foo (); }
    virtual void foo () { std::cout << "A::foo" << std::endl; }
};

struct B : public A
{
    virtual void foo () { std::cout << "B::foo" << std::endl; }
};


int
main ()
{
    B b;      // prints "A::foo"
    b.foo (); // prints "B::foo"
    return 0;
}

While it s implementation dependent, it would actually have to happen before the body of the constructor itself is evaluted since you are allowed, per the C++ spec (12.7/3), to access non-static class methods via the this pointer in the constructor body ... therefore the vtable would have to be setup before the body of the construtor is called, otherwise calling virtual class methods via the this pointer would not work correctly. Even though the this pointer and the vtable are two different things, the fact that the C++ standard allows the this pointer to be used in the body of a constructor demonstrates how the compiler must implement the vtable for standards-compliant uses of the this pointer to work correctly, at least from a timing perspective. If the vtable were initialized during or after the constructor body is called, then using the this pointer to call virtual functions inside the body of the constructor or pass the this pointer to functions that depend on dynamic dispatch would be problematic and create undefined behavior.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签