Dynamic Binding

C

compgeek.320

Hi Everyone,

Can any one explain me about Dynamic binding related to OOPs concepts.I
studied in a book that the polymorphic fuction call will be decided in
the runtime rather than the compile time.
My doubt is that
During compilation the data types of the arguments are known to us
the compiler will store the starting address of the fuction based on
the arguments
Where is the concept of dynamic binding used??

Please explain me in detail with an example

Bye
 
I

Ivan Vecerina

: Hi Everyone,
:
: Can any one explain me about Dynamic binding related to OOPs concepts.I
: studied in a book that the polymorphic fuction call will be decided in
: the runtime rather than the compile time.
: My doubt is that
: During compilation the data types of the arguments are known to us
: the compiler will store the starting address of the fuction based on
: the arguments
: Where is the concept of dynamic binding used??

When you make a virtual call to a member function in C++,
the code looks up the address of the target function in
a table associated with the specific object instance.
I.e:

class Shape
{
public:
virtual void draw()=0;
};

void drawMany( Shape const& shape, int howManyTimes )
{
for( int i = 0 ; i<howManyTimes ; ++i )
shape.draw();
}

drawMany doesn't know how to draw an instance of a specific
subclass of class Shape, but will look-it-up within the object.

Any C++ textbook will provide specific and complete examples.
e.g. http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Ivan
 
X

xjmshanghai

You think the compiler should be smart enough to know which and where
foo() should be called. Actually in writing C++, there are too many
things need in consideration, it's diffcult to control all the world.
Make mechanism simpler is better. BTW, I think dynamic binding can less
compile time, this can prompt efficiency of programming.
 
I

Ivan Vecerina

: You think the compiler should be smart enough to know which and where
: foo() should be called. Actually in writing C++, there are too many
: things need in consideration, it's diffcult to control all the world.
: Make mechanism simpler is better. BTW, I think dynamic binding can less
: compile time, this can prompt efficiency of programming.

The actual type of an object passed/accessed by pointer/reference often
cannot be known until execution time.

For example, you may have a:
std::vector<Shape*> shapes;
A file-loading code may populate the vector with pointers to a
variety of subclasses of Shape, based on what is specified in
the file.

Then some code can traverse the vector and ask each shape
to draw itself:
for(... i = 0 ; i<shapes.size() ; ++i )
shapes->draw();

The compiler will have to check some information within
each instance being processed to know which actual drawing
routine needs to be called.
On most platforms, this can even work with subclasses of
Shape being implemented in dynamically loaded libraries.


Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top