Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you're right, in C++ there are two recognized "types" of polymorphism. And they mean pretty much what you think they mean</p> <h1>Dynamic polymorphism</h1> <p>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)</p> <h1>Static polymorphism</h1> <p>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</p> <pre><code>template &lt;typename T&gt; T add(const T&amp; lhs, const T&amp; rhs) { return lhs + rhs; } </code></pre> <p>If this had been dynamic polymorphism, then we would define the <code>add</code> 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 <em>know</em> which type is passed to the function. The exact type is only known at runtime, hence this is <em>dynamic</em> polymorphism.</p> <p>Here, though, we don't require you to derive from anything, the type <code>T</code> just has to define the <code>+</code> operator. It is then inserted <em>statically</em>. So at compile-time, we can switch between any valid type as long as they <em>behave</em> the same (meaning that they define the members we need)</p> <p>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" <em>concept</em>.</p> <p>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.</p> <p>However, when a C++ programmer <em>just</em> say polymorphism, they generally refer to dynamic/runtime polymorphism.</p> <p>(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)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload