Yes, you are right, in C ++ there are two recognized “types” of polymorphism. And they mean pretty much what you think they mean
Dynamic polymorphism
- this is what C # / Java / OOP people usually call simply "polymorphism." It is essentially a subclass, either based on the base class, or overriding one or more virtual functions, or implementing the interface. (which in C ++ is performed by overriding virtual functions belonging to an abstract base class)
Static polymorphism
takes place at compile time and can be considered a variation of the duck. The idea here is that different types can be used in a function to represent the same concept, even though it is not completely connected. For a very simple example, consider this
template <typename T> T add(const T& lhs, const T& rhs) { return lhs + rhs; }
If it was a dynamic polymorphism, we would define an add function to take some "IAddable" object as arguments. Any object that implements this interface (or derived from this base class) can be used, despite their various implementations, which gives us polymorphic behavior. We don’t care what type is passed to us if it implements some kind of “can be added together” interface. However, the compiler does not actually know what type is passed to the function. The exact type is known only at runtime; therefore, it is a dynamic polymorphism.
Here, however, we do not require you to extract from anything, the type T just needs to define the + operator. Then it is inserted statically. Therefore, during compilation, we can switch between any valid type if they behave the same way (which means that they define the members that we need)
This is another form of polymorphism. In principle, the effect is the same: the function works with any implementation of the concept that interests us. We don’t care whether the object we are working on is a string, int, float or a complex number, since it implements the concept of “can be added together”.
Since the type used is known statically (at compile time), this is known as static polymorphism. And the path of static polymorphism is achieved through templates and function overloading.
However, when a C ++ programmer simply talks about polymorphism, they usually refer to dynamic / temporary polymorphism.
(Note that this is not necessarily true for all languages. A functional programmer usually means something like static polymorphism when he uses the term - the ability to define common functions using some parameterized types similar to patterns)