本文介绍了如何虚函数表在C ++和C#实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让有这种情况(在C ++,C#中类A,B是接口):

Lets have this situation (in c++, in c# classes A,B are interfaces):

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#编译器总是创建一个虚函数表?它铸造的时候作出任何指针的修正?

Does the c# compiler create always one vtable? Does it make any pointer fixups when casting?

推荐答案

不要过于迂腐,但C#编译器不会涉足这个水平。整个模型的类型,继承,接口实现等等。实际上是由CLR更具体的CTS(通用类型系统)来处理。 .NET编译器大多只是生成表示这是后来被其中所有V表处理等是照顾的CLR执行的意图IL代码。

Not to be overly pedantic, but the C# compiler does not get involved at this level. The Entire type model, inheritance, interface implementation etc. is actually handled by the CLR more specifically the CTS (Common Type System). .NET compilers mostly just generate IL code that represents intent which is later executed by the CLR where all Vtable handling etc. is taken care of.

有关如何在一些细节CLR创建和管理运行时类型下面的链接将是一个很好的起点。接近尾声的方法表和接口映射进行了​​说明。

For some detail on how the CLR creates and manages runtime types the following link will be a good starting point. Towards the end the MethodTable and Interface Maps are explained.http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

这篇关于如何虚函数表在C ++和C#实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:06