简体   繁体   English

是强制 static 还是动态多态性?

[英]Is coercion static or dynamic polymorphism?

Parametric polymorphism and overloading are static polymorphism, because the compiler knows at compile-time which function to call.参数多态性和重载是 static 多态性,因为编译器在编译时知道要调用哪个 function。 Subclassing is dynamic polimorphism, beause the function gets determined at run-time.子类化是动态多态性,因为 function 在运行时确定。 But what is coercion (implicit casting)?但是什么是强制(隐式强制转换)? Static or dynamic polimorphism? Static 还是动态多态? The compiler knows at compile time which function to call, but the actual cast happens at run-time.编译器在编译时知道要调用哪个 function,但实际的转换发生在运行时。 Or is that statement wrong?还是那句话是错的?

Runtime polymorphism involves (potentially) several distinct bits of machine code being selected from based on some runtime data related to the runtime type of data involved, and that selection happens at runtime.运行时多态性涉及(可能)基于与所涉及的运行时数据类型相关的一些运行时数据从中选择几个不同的机器代码位,并且该选择发生在运行时。 (I say potentially because you can use virtual dispatch when there's only one concrete derived type, but the runtime mechanism is there to support further types). (我说可能是因为您可以在只有一种具体派生类型时使用虚拟调度,但运行时机制可以支持更多类型)。

With coercion, only one machine code path is required to massage the data into some other type needed by the code then executed - there is no runtime type-based selection of the code to execute.使用强制,只需要一个机器代码路径来将数据按摩成随后执行的代码所需的其他类型 -没有基于运行时类型的代码选择来执行。 What that one machine code path should be is decided at compile time.一个机器代码路径应该是什么是在编译时决定的。

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

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