Thursday, July 9, 2009

C++: Differences between Static Type and Dynamic Type of a variable?

Can you please give an example as well?

C++: Differences between Static Type and Dynamic Type of a variable?
1 difference is that static is stored in data segments whereas static is stored in heap..





static is intialized only once........


memory allocated to static at compile time whereas dynamic is allocated at runtime....


static has local scope but lifetime is till program executes.....
Reply:In general, C++ variables are statically typed. The closest you get to dynamic types is polymorphism, which is where you have an instance of a class that might belong to one of the subclasses. For example, you might have a class Shape with a method draw.





Circle *c;


Shape *s;


s = c;


s-%26gt;draw();





So calling s.draw() is actually calling Circle's draw function. The dynamic part means that if you were given another Shape x but didn't know if it was a circle, a square, etc, it would still draw a circle.

anemone

No comments:

Post a Comment