[HELP] Polymorphism in C++?

I’m having quite a lot of trouble understanding polymorphism.
From what I understand polymorphism can be categorized into compile time and run time polymorphism.

Compile-time has function overloading and operator overloading, whereas run time has virtual functions (according to this).
Also, there is preprocessing, overloading, templates, and virtual dispatch (here) … plus ad-hoc, parametric, subtype (aka inclusion), and coercion (aka casting) (here). In addition, in Sam’s video, he also talks about sub-classes?
Just getting a bit confused with all the names flying around…
Which names are synonyms and which are distinct concepts? And maybe some examples would be helpful.

Thanks in advance!

The stackoverflow link goes over everything in detail I don’t think I could expand on. The top answer goes all the concepts of polymorphism and how you can achieve that with C++ features.

I don’t really understand the stackoverflow link to be honest.
What is the difference between “mecahnisms for polymorphism” and “types” (ad-hoc, parametric, etc.) that he talks about? In Sam’s video he only talks about the “types,” so how do the “mechanisms” connect?

Also, another possibly unrelated question: are casts in polymorphism the same as using Cast<>() in Unreal?

Thanks again

“mechanisms” being C++ features. e.g. templates, function overloading etc.

Could you expand on this question because casts are orthogonal to polymorphism. Casting is to say treat this type as another type. With that said, that is how you would cast from a parent to a derived in Unreal.

First of all, my question about polymorphism:
@DanM, so when I am using polymorphism what I’m writing in Visual Studio is: a template (i.e. mechanism) or would I be writing ad-hoc? Or rather, just an example, can templates be categorized as ad-hoc? Or, third option, are these two (i.e. mechanisms and categorizations) completely unrelated? Or is it something else, like that ad-hoc can be any of the four mechanisms?

Basically what I want to know is do you have 7 types of polymorphism?

  1. preprocessing
  2. overloading
  3. templates
  4. virtual dispatch
  5. ad-hoc
  6. parametric
  7. subtype

Or do you have 3 which each has its own mechanisms?

  1. Ad-hoc (< just examples btw)
    a. Template
    b. etc…
  2. Parametric
    a. …
  3. Subtype
    a. …

There’s the concept of polymorphism which you can categorise into run time polymorphism and compile time which you can then further subcategorise

  • Run Time:
    • Subtype
  • Compile Time:
    • Ad-hoc

That’s all theory. The implemtation (i.e. C++'s mechanisms) to achieve those would be function overloading, templates, virtual functions etc.

Thank you. That does clear it up a bit, but where would parametric polymorphism and subtype polymorphism be categorized?

Honestly it really doesn’t matter that much, 9 times out of 10 when someone says “polymorphism” they mean inheritance, virtual functions, and dynamic dispatch. They will just refer to the others as the language feature.

Though to answer your question parametric would be compile time (templates) and subtype would be runtime (dynamic dispatch - I put the wrong thing before and edited my comment).

Privacy & Terms