C++ 中的参数多态性和包含多态性是什么?
我正在地址 https 阅读一些 C++ 文本://cs.senecac.on.ca/~chris.szalwinski/archives/btp200.082/content/adhoc.html。
在UNIVERSAL POLYMORPHISM
一节中,作者提到了Parametric
和Inclusion
多态性。我不太确定我是否理解这一点,特别是为什么Parametric
多态性是在编译时实现的,而Inclusion
多态性是在运行时实现的?
任何人都可以给我一个明确的解释或例子吗?
I am reading some C++ text at the address https://cs.senecac.on.ca/~chris.szalwinski/archives/btp200.082/content/adhoc.html.
In the section UNIVERSAL POLYMORPHISM
, the author mentioned about Parametric
and Inclusion
polymorphisms. I am not quite sure that I understand the point, especially why Parametric
polymorphism is implemented at compile time while Inclusion
polymorphism is implemented at run time?
Can anyone give me a clear explanation or an example, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++ 中的“参数多态性”表示模板。
我认为 C++ 中的“包含多态”是指标准所指的多态:虚拟方法、子类等。
我认为这些名字很笨拙,而且带有学术气息。
"Parametric polymorphism" in C++ means templates.
I think that "inclusion polymorphic" in C++ means polymorphic the way the Standard refers to it: virtual methods, subclasses and the like.
I think the names are clumsy and smack of academia.
我认为“参数化”指的是方法/函数重载 - 我们可以通过查看参数的数据类型来确定在编译时使用哪种方法。
“包含”意味着
方法/函数重写
- 在父子类关系中,如果父类和子类具有相同的函数,那么它将在运行时确定(取决于对象类型) )将调用什么方法。I think by 'Parametric' it refers to
method/function overloading
- we can determine what method is to be used at compile time by looking at the datatype of it's parameters.And by 'inclusion' it means
method/function overriding
- in a parent sub-class relation, if both parent and child class have the same function then it would be determined at runtime (depending on the object type) what method would be invoked.我理解通用多态性与我们在 C++ 中所期望的不同。 C++ 是临时多态性。
Universal 表示,无论类型数量如何,同一签名只能有多个版本。
我认为其他答案忽略了参数和包含是通用类别的细节。鉴于原文,我可以看出他们或我是如何感到困惑的。 ;)
给出以下内容:
参数将类似于:
FooBar 中包含的签名在编译时静态确定。
C++ 不直接支持包含多态性。它们更接近于您可能在函数是一阶的脚本语言中发现的注入。
请不要从字面上理解此代码,它只是用于演示。
FooBar
在编译时并不知道它的接口,它是动态组合的。我在 javascript 和 Lua 中使用过类似的行为,并且我确信许多其他人也有类似的行为。I understood universal polymorphism to be different from what we expect in C++. C++ is ad-hoc polymorphism.
Universal says there can be only version of the same signature, regardless of the number of types.
I think the other answers skim over the detail that parametric and inclusion are categories of universal. Given the original text, I can see how they or I have been confused. ;)
Given the below:
Parametric would be like:
The signatures contained in
FooBar
is statically determined at compile time.C++ does not directly support inclusion polymorphism. They would be closer to injection that you might find in scripting languages where functions are first order.
Please don't take this code literally, it is just for demonstration.
FooBar
doesn't know its interface at compile time, it is dynamically composed. I've used similar behavior in javascript and Lua, and I'm sure many others have the like.