编译时多态性和运行时多态性 [英] compile time polymorphism and runtime polymorphism

查看:215
本文介绍了编译时多态性和运行时多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到某处的多态性只是指虚拟函数。但是,它们包括函数重载和模板。后来,我发现有两个术语,编译时多态性和运行时多态性。真的吗?



我的问题是,当我们一般性地讨论多态性时,广泛接受的意思是什么?

解决方案

是的,你是对的,在C ++中有两个公认的类型的多态性。



动态多态性

是什么C#/ Java / OOP人通常简称为多态性。它基本上是子类,从基类派生并覆盖一个或多个虚拟函数,或实现接口。 (在C ++中通过重写属于抽象基类的虚函数来完成)



静态多态性



在编译时发生,并且可以被认为是鸭型的变化。这里的想法是简单的,尽管是完全不相关的,不同的类型可以在函数中使用以表示相同的概念。对于一个非常简单的例子,考虑这个

  template< typename T& 
T add(const T& lhs,const T& rhs){return lhs + rhs; }

如果这是动态多态性,那么我们将定义 add 函数采用某种IAddable对象作为其参数。可以使用实现该接口(或从该基类派生)的任何对象,尽管它们具有不同的实现,这为我们提供了多态行为。我们不关心传递给我们的类型,只要它实现某种可以添加在一起接口。
然而,编译器实际上并不知道哪个类型被传递给函数。确切的类型只在运行时才知道,因此这是动态多态性。



这里,我们不需要你从什么,类型 T 只需要定义 + 运算符。然后静态插入 。因此,在编译期,只要它们的行为相同(意味着它们定义了我们需要的成员),我们就可以在任何有效类型之间切换。



这是另一种形式的多态性。原则上,效果是一样的:该函数与我们感兴趣的概念的任何实现一起工作。我们不关心我们工作的对象是一个字符串,一个int,一个浮点数或复数,如只要它实现了可以一起加入。

由于使用的类型是静态的(在编译时),因此,称为静态多态性。实现静态多态性的方式是通过模板和函数重载。



然而,当一个C ++程序员只是说多态性时,动态/运行时多态性。



(注意,这并不一定适用于所有语言。函数式程序员通常使用静态多态性,使用某种类型的参数化类型定义通用函数的能力,类似于模板)


I noticed that somewhere polymorphism just refer to virtual function. However, somewhere they include the function overloading and template. Later, I found there are two terms, compile time polymorphism and run-time polymorphism. Is that true?

My question is when we talked about polymorphism generally, what's the widely accepted meaning?

解决方案

Yes, you're right, in C++ there are two recognized "types" of polymorphism. And they mean pretty much what you think they mean

Dynamic polymorphism

is what C#/Java/OOP people typically refer to simply as "polymorphism". It is essentially subclassing, either deriving from a base class and overriding one or more virtual functions, or implementing an interface. (which in C++ is done by overriding the virtual functions belonging to the abstract base class)

Static polymorphism

takes place at compile-time, and could be considered a variation of ducktyping. The idea here is simply that different types can be used in a function to represent the same concept, despite being completely unrelated. For a very simple example, consider this

template <typename T>
T add(const T& lhs, const T& rhs) { return lhs + rhs; }

If this had been dynamic polymorphism, then we would define the add function to take some kind of "IAddable" object as its arguments. Any object that implement that interface (or derive from that base class) can be used despite their different implementations, which gives us the polymorphic behavior. We don't care which type is passed to us, as long as it implements some kind of "can be added together" interface. However, the compiler doesn't actually know which type is passed to the function. The exact type is only known at runtime, hence this is dynamic polymorphism.

Here, though, we don't require you to derive from anything, the type T just has to define the + operator. It is then inserted statically. So at compile-time, we can switch between any valid type as long as they behave the same (meaning that they define the members we need)

This is another form of polymorphism. In principle, the effect is the same: The function works with any implementation of the concept we're interested in. We don't care if the object we work on is a string, an int, a float or a complex number, as long as it implements the "can be added together" concept.

Since the type used is known statically (at compile-time), this is known as static polymorphism. And the way static polymorphism is achieved is through templates and function overloading.

However, when a C++ programmer just say polymorphism, they generally refer to dynamic/runtime polymorphism.

(Note that this isn't necessarily true for all languages. A functional programmer will typically mean something like static polymorphism when he uses the term -- the ability to define generic functions using some kind of parametrized types, similar to templates)

这篇关于编译时多态性和运行时多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆