c++ - compile time polymorphism and runtime polymorphism -


I saw that polymorphism refers only to the virtual function somewhere. However, they include function overloading and templates. Later, I found that there are two conditions, compile time polymorphism and run-time polymorphism. is it true?

My question comes when we usually talk about polymorphism, what is the widely accepted meaning?

Yes, you are right, there are two recognized "types" of polymorphism in C ++ and they Very much mean what you think

dynamic polymorphism

Whether C # / Java / OOP people are commonly referred to as "polymorphism" is essentially subclassing , Either from one base class and overriding one or more virtual functions, or implementing an interface. (Which is done by overriding the virtual tasks related to the abstract base class in C ++)

static polymorphism

occurs at compiled time, and it should be considered as a variation of decultiping Could. The idea here is that, despite being completely unrelated, different types of functions can be used to represent the same concept. For a very simple example, consider this

  template & lt; Typename T & gt; T plus (CONST T & LHS, CONST T & RA) {return LHS + RS; }  

If this was dynamic polymorphism, then we defined the add function to take any kind of "idle" object as its debate will do. Any object that implements that interface (or received from that base class) can be used in spite of their separate implementation, which provides us with polymorphic behavior. We do not care how we are passed, until it implements some kind of "can be connected together" interface. However, the compiler does not actually know how the function is passed, the exact type is known only on the runtime, so it is dynamic polymorphism.

Here, however, we do not have to get anything for you, type T has to define only the + operator. After this, it is inserted statically . So at the time of compilation, we can switch between any valid type as long as they behave the same (which means they define our members)

< P> This is another form of polymorphism. In theory, the effect is the same: This task works with any implementation of the concept that we are interested in. We do not care that the object we work on is a string, int, float, or a complex number concept

Since it is used, it Static (at compile-time) is known, it is called static polymorphism. And the way static polymority is obtained, it is received through templates and functions overloading.

However, when a C ++ programmer is called bus polymorphism, they usually refer to dynamic / runtime polymorphism.

(Note that this is not true for all languages. A functional programmer usually means something like static polymorphism, when it uses the word - using certain types of parametric types Ability to define common tasks, similar to templates)


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -