Hi people
Could someone tell me what is the difference between runtime and compiletime polymorphism?
thanks alot

Ancient Dragon is an idiot and doesn't know what he's talking about. Look up the terms online and you'll find definitions, and you can use your brain to see how they're different.

commented: He atleast gave some help by providing that pointer. We don't need your advice about who is an idiot and who is not. -1
commented: If you blame AD, your against me!! (and that rimes) -1
commented: you are retarded yourself -1

Ancient Dragon is an idiot and doesn't know what he's talking about. .

Agreed -- had a Senior Moment :)

What? I'm pretty sure everybody is an idiot and most of the time most people don't know what they're talking about. I don't know what I am talking about usually. That has nothing to do with being senior.

>I'm pretty sure everybody is an idiot and most of the time most people don't
>know what they're talking about.
I don't think so. Most of the time I know what I am talking about. It is just that very rarely (usually when there is an emotional pressure) I speak rubbish.
>I don't know what I am talking about usually.
Thanks for telling. But could you help us by marking, on your post, whether or not you are serious?
>That has nothing to do with being senior.
Not likely. Programming is greatly influenced by experience. I don't say seniority, but it must be the experience that matters. It is no doubt that Ancient is very experienced than you are.
If only you would have read the pointer he passed, you would have find that in c++, compile time polymorphism is referred to as 'overloading' while the run-time polymorphism is known as "polymorphism".

To the OP: As I pointed above,

Overloading means using the same operation to "invoke different code", you
say, and polymorphism uses the same "piece of code to operate on objects of
different types".

Polymorphism and Overloading are often given a parent-child relationship:"Overloading is a kind of polymorphism"

Static Polymorphism (the compile-time) means basically those language structure which will cause the compiler to produce code at the compile-time. That is, the compiler is well aware that what code is to be generated at the compile-time itself. The example includes: specifically overloading of operators, functions.

The dynamic Polymorphism(the runtime) means that the compiler is unaware what code is to be generated so it binds the possible code and let the program decide it at the run-time itself.
The example includes the virtualness of a class member or the entire class itself.
Consider a file : shape.h which contain a class Shape and 3 child(inherited) class : Square, Circle, Triangle.
Each of them has a member function called Draw which will draw that particular shape to the screen.
I declare the Draw of Shape as virtual. This will get me a great advantage.
The user of the class will be able to do this:

//this is main.cpp
Shape* S[3];
S[0]=new Square; S[1]=new Triangle; S[2]=new Circle;
for(int i=1;i<3;++i) S[i]->Draw();

The above code will draw different Shape's for each of the different calls.

As you know, the Shape.h will be compiled differently, hence the compiler won't know what code should be generated when someone calls Shape->Draw(). Hence , he delays(whence the name Late Binding) the code generation.

Hope this helps!

Static Polymorphism (the compile-time) means basically those language structure which will cause the compiler to produce code at the compile-time. That is, the compiler is well aware that what code is to be generated at the compile-time itself. The example includes: specifically overloading of operators, functions.

And template metaprogramming.

As you know, the Shape.h will be compiled differently, hence the compiler won't know what code should be generated when someone calls Shape->Draw().

Uh, no, the compiler knows exactly what code to generate: something that looks up a function pointer specified by the object and calls that function.

Hence , he delays(whence the name Late Binding) the code generation.

Uh, no. Code isn't generated at runtime when calling virtual functions.

I guess you were under emotional pressure when making that post.

commented: Your grammar is much better than the person below you. And you're from Bangalore too Sammy! +20

>And template metaprogramming.
Template metaprogramming is an entirely different concept. It aims at compile-time code execution so as to save the run-time cost. Using template metaprogramming will force the compiler to generate the output of a template metaprogrammed function at the compile-time itself.
For Example of template metaprogramming, this code will generate fabonaci number at the compile-time itself.

template <int N>
struct Factorial 
{
    enum { value = N * Factorial<N - 1>::value };
};
 
template <>
struct Factorial<0> 
{
    enum { value = 1 };
};
 
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}

If you can infer, this will save much run-time costs.
Overloading does not execute any code. It merely means generating different code for a similar interface.

>Uh, no, the compiler knows exactly what code to generate: something that
>looks up a function pointer specified by the object and calls that function.
Yeah, but does it knows which Draw() to call at the compile time? No.
>Uh, no. Code isn't generated at runtime when calling virtual functions.
Yes, the code isn't generated but it is binded dynamically.

If you think you have conquered the world correcting me above, you might need to re-think. The OP would surly confused if I had gone too deep in the explanation.
My above posts highlights the fact that "the compiler doesn't know, at the compile time, which Draw() to call."


>I guess you were under emotional pressure when making that post.
No I wasn't.

To OP and and you: please read http://www.parashift.com/c++-faq-lite/virtual-functions.html to gain some more insight.
(Although, it would be slightly different from the topic since the OP's question was regarding polymorphism and not virtual-ness)

Hey sidd aaaaaaah...does OP mean me?
and thank you for the link dragon

Yes.
OP=Original Poster and on this thread it is you. Congratulations ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.