Compile-time polymorphism and run-time polymorphism

I noticed that somewhere polymorphism simply refers to a virtual function. However, somewhere they include function overloading and pattern. Later, I discovered that there are two terms, compile-time polymorphism and run-time polymorphism. It's true?

My question is when we talked about polymorphism in general, what is the widespread meaning?

+7
c ++
source share
8 answers

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)

+13
source share

"Polymorphism" literally means "many forms." This term, unfortunately, is a bit overloaded in computer science (sorry for the pun).

According to FOLDOC , polymorphism is "a concept first identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types to list for example anything.

In general, this is a “programming language function that allows the processing of values ​​of different data types using a single interface” to quote Wikipedia , which further describes two main types of polymorphism:

Parametric polymorphism is when the same code can be applied to several data types. Most people in the object-oriented programming community call this "generic programming" rather than polymorphism. Generics (and to some extent templates) fall into this category.

Polymorphism

Ad-hoc is when different code is used for different data types. Overload falls into this category, as does redefinition. This is what people in the object-oriented community usually refer to when they say "polymorphism." (and, in fact, many averages redefine rather than overload when they use the term "polymorphism")

For ad-hoc polymorphism, the question also arises as to whether the resolution of the implementation code occurs at runtime (dynamic) or compilation time (static). Method overloading is usually static, and method overriding is dynamic. This is where the terms static / compilation polymorphism and dynamic / temporary polymorphism come from.

+8
source share

Usually people refer to run-time polymorphism in my experience ...

+5
source share

When a C ++ programmer says "polymorphism," it most likely means a subtype polymorphism, which means "late binding" or "dynamic binding" to virtual functions. Function overloading and general programming are both instances of polymorphism, and they involve static binding at compile time, so they can be called collectively polymorphism of compilation time. Subtype politism is almost always referred to as simply polymorphism, but the term can also refer to all of the above.

+5
source share

In its most concise form, polymorphism means the ability of one type to look like it is another type.

There are two main types of polymorphism.

  • Subtype politicism: if D comes from B , then D is B
  • Interface polymorphism: if C implements interface I

First, this is what you think of as run-time polymorphism. The second does not apply to C ++ and is indeed a concept that applies to Java and C #.

Some people think of operator overloading ( + , - , / , * ) in a special case as a type of polymorphism, since it allows you to think about types that overloaded these operators (i.e. + for string and + for int ). This concept of polymorphism is most often applied to dynamic languages. I consider this an abuse of terminology.

Regarding programming patterns, you will see that some use the term “polymorphism”, but this is really a completely different matter than what we usually mean by polymorphism. The best name for this concept is “general programming” or “general”.

+3
source share

Different types of function overloading (compilation time polymorphism ... June 9, 2011 ... Polymorphism means that the same object behaves differently at different times. Compilation time polymorphism is also called static binding. Http://churmura.com/technology / programming / various-types-of-function-overloading-compile-time-polymorphism-static-binding / 39886 /

+1
source share

A simple explanation of compilation time polymorphism and runtime polymorphism from: questionscompiled.com

Compilation time Polymorphism:

C ++ support polymorphism. One function is a multiple purpose, or, in short, many functions that have the same name, but with a different body of the function. For each function call, the compiler associates the call with one function definition at compile time. This decision of binding between several functions is taken from consideration of the formal arguments of the function, their data type and their sequence.

Runtime Polymorphism:

C ++ allows you to bind to a delay until runtime. When you have a function with the same name, an equal number of arguments and the same data type in the same sequence in the base class, as well as in the derived class and the form function call: base_class_type_ptr-> member_function (args); will always call the member function of the base class. The virtual keyword for a member function in the base class tells the compiler to delay the binding to runtime.

Each class that has at least one virtual function has a vtable that helps you bind at runtime. If you look at the contents of a pointer to a base class type, it will correctly call a member function of one of the possible member functions of a derived / base class.

+1
source share

Yes, you are basically right. Polymorphism of compilation time is the use of patterns (instances of which are different but fixed at compile time), while polymorphism at runtime refers to the use of inheritance and virtual functions (instances of which are different and fixed at runtime).

0
source share

All Articles