简体   繁体   English

为什么Struct不能从另一个struct派生?

[英]Why a Struct can not be derived from another struct?

I am more interested in an answer from the .Net and CLR point of view: 我对.Net和CLR的观点回答更感兴趣:

Why a struct can not be a base class of another struct or vise versa? 为什么struct不能是另一个struct的基类,反之亦然?

Structs occupy fixed-size slots in the stack (or wherever they're living). 结构占据堆栈中的固定大小的插槽(或者它们居住的任何地方)。

Therefore, you wouldn't be able to do any kind of polymorphism with structs, since the derived struct would be a different size. 因此,您将无法对结构进行任何类型的多态,因为派生的结构将是不同的大小。

It would be possible to inherit members from other structs, but since you wouldn't be able to do any kind of polymprphism, it wouldn't be worth the confusion. 有可能从其他结构继承成员,但由于你不能做任何形式的多重现象,所以不值得混淆。

In .net, if class A inherits from type B and an object of type A is passed to code which expects an object of type B , the passed object would not be converted to a B but would remain an A . 在.net中,如果类A继承自类型B并且类型A的对象被传递给期望类型为B的对象的代码,则传递的对象将不会被转换为B但仍将是A This is possible because every object has stored with it a reference to a type descriptor. 这是可能的,因为每个对象都存储了对类型描述符的引用。 Classes do not have any type descriptors stored with them. 类没有与它们一起存储的任何类型描述​​符。 If a struct of type A could be passed by value to a routine expecting a struct of type B , it would become a struct of type B . 如果类型A的结构可以通过值传递给期望类型为B的结构的例程,那么它将成为类型B的结构。 In cases where that would be sensible, it would be more practical to define a widening conversion operator from type A to B . 如果这是明智的,那么从AB类定义扩展转换运算符会更实际。

There are times it might be useful to pass a struct of type A by reference to a routine expecting type B . 有时,通过引用传递类型A的结构来预期类型B可能是有用的。 Conversion operators wouldn't help there. 转换运营商不会帮助那里。 That could however be handled, albeit somewhat awkwardly, by having struct type A contain nothing but a field of type B . 然而,通过使结构类型A包含类型B的字段,可以处理,尽管有点笨拙。 That field could then be passed by reference to the routine expecting type B ; 然后可以通过引用期望类型B的例程来传递该字段; the difficulty would be that one would have to include the name of the inner struct in any field accesses. 困难在于,必须在任何字段访问中包含内部结构的名称。

What would be helpful, not just for structs but for classes as well, would be the concept of an 'extension type'. 什么是有用的,不仅是结构,而是类,也将是“扩展类型”的概念。 All classes, whether inheritable or not, could be extended with an extension type, which would include only instance members; 所有类,无论是否可继承,都可以使用扩展类型进行扩展,扩展类型只包含实例成员; variables or values of an extension type would be regarded as run-time objects of the base type, and all conversions between extension types and their base types, or between extension types derived from the same base type would be considered 'widening' and would be processed as no-ops. 扩展类型的变量或值将被视为基类型的运行时对象,扩展类型与其基类型之间或从相同基类型派生的扩展类型之间的所有转换都将被视为“加宽”,并且将是作为无操作处理。 The only effect of an extension type would be to bring into scope members defined for that type. 扩展类型的唯一影响是引入为该类型定义的范围成员。 These would behave much like extension methods, except that they would only apply to variables and variables declared as the extension type, and the members of extension types would have priority over the base class members. 这些行为与扩展方法非常相似,只是它们只适用于声明为扩展类型的变量和变量,扩展类型的成员优先于基类成员。 One can imagine many uses for such things (in cases where one would like to use extension methods on some instances of a class, but may not want them available on all instances), but as yet no language I'm aware of supports such a feature. 可以想象很多用于这类事情(如果有人想在类的某些实例上使用扩展方法,但可能不希望它们在所有实例上都可用),但是至于我所知道的语言还没有支持这样的特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM