让我们来看一个情况(在C++中,在C#中,类A、B是接口):
class A { virtual void func() = 0; };
class B { virtual void func() = 0; };
class X: public A, public B { virtual void func(){ var = 1; } int var;};
X * x = new X; // from what I know, x have 2 vtables, is this the same in c#?
A * a = (A*)x; // a == x
B * b = (B*)x; // here b != x, so when calling b->func(), how is the address of var correct?
c#编译器是否总是创建一个vtable?在进行类型转换时,它是否会进行任何指针修复?
a == x
和b != x
。由于指针在堆栈上一个接一个地分配,它们都有不同的地址...尽管它们应该都指向同一个对象。 - PypeBros