0

Compile time polymorphysm or runtime

Hi Refer code below: I know it is static / compile time polymorphism, but question was about input at run time. If input is decided at runtime, which function to get called is also at run time decision. Can someone now plz resonate that why this code represent static / compile time polymorphism? https://sololearn.com/compiler-playground/ceRUzz989vOe/?ref=app P.s. why then same is not decided by compiler for run time polymorphism at time of compilation?

13th Feb 2025, 5:30 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Answers
+ 2
Ketan Lalcheta I agree about the compilation due to working more on python I got confused with interpretation. Well both runtime and compile type polymorphism has different approaches as runtime for different classes with same function which uses virtual functions there are pure functions with no arguments and one more with arguments. So that's different scenarios. You can't mix it with compile time polymorphism.
13th Feb 2025, 11:20 AM
Aysha
+ 2
Ketan Lalcheta Your sample code is able to completely compile the main and both add functions at compile time. It is not dependent on any runtime input that would change how the functions are compiled. Compiling simply refers to rendering the code down into bytecodes which can be processed by the CPU. Which add function gets called may depend on runtime input, but calling a function is distinct from compiling it.
13th Feb 2025, 11:12 PM
Shardis Wolfe
+ 1
Run time polymorphism are based on same function for different classes which are solved using virtual functions and compile time polymorphism are based on function overloading and operator overloading. That's the reason both work differently. The given codes inputs will be used in runtime as it waits for the inputs and then it gets compiled and gives the outputs accordingly. Check out the link below to get details about them. https://www.geeksforgeeks.org/compile-time-vs-run-time-polymorphism-difference-in-cpp/
13th Feb 2025, 7:25 AM
Aysha
0
Aysha , I am clear about virtual keyword. We can't say that lack of virtual means it is compile time polymorphism as a logical reasoning to the developer. Also, it wait for input and then gets compiled is something wrong.
13th Feb 2025, 11:07 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks for sharing. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one. Thanks for sharing such great information, I highly appreciate your hard-working skills as the post you published have some great information which is quite beneficial for me, I hope you will post more like that in the future. https://www.tdcj-inmatesearch.com
14th Feb 2025, 4:28 AM
reyna789