Very basic question on Dynamic binding?

P

prashna

Hi all,

Is'nt a function invocation through a function pointer is dynamic
binding?
For example consider the following program

1 int main()
2 {
3 int (*fun_ptr)();
4 int fun(){printf("HIIIIII\n");}
5
6 fun_ptr = fun;
7
8 fun_ptr();
9 return 0;
10 }

Consider the statement at line 8..I guess, which function to be
invoked,at line 8, will be decided at runtime..Is this what is called
as dynamic binding or polymorphism or is it different from
this?..Totally confused with this concept..anybody please help me..
 
K

Karl Heinz Buchegger

prashna said:
Hi all,

Is'nt a function invocation through a function pointer is dynamic
binding?
For example consider the following program

1 int main()
2 {
3 int (*fun_ptr)();
4 int fun(){printf("HIIIIII\n");}
5
6 fun_ptr = fun;
7
8 fun_ptr();
9 return 0;
10 }

OK. I consider (minus the obvious error, of course. C doesn't have
nested functions :)
Consider the statement at line 8..I guess, which function to be
invoked,at line 8, will be decided at runtime..Is this what is called
as dynamic binding or polymorphism or is it different from
this?

No. In principle you got it.
Polymorphism means that the decision on which function to actually
call is delayed until runtime when the exact type of the object
to use for the call can be determined.

In the above there is no object, it's a plain vanilla function call.
But the principle is the same: The decission which function to call
is delayed until runtime.

Example:

class Animal
{
public:
virtual void MakeNoise() { cout << "Don't know which noise to make\n" );
};

class Dog: public Animal
{
public:
virtual void MakeNoise() { cout << "Wuff, wuff\n" );
};

class Cat: public Animal
{
public:
virtual void MakeNoise() { cout << "Miau, miau\n" );
};

void React( Animal& Pet )
{
Pet.MakeNoise();
}

int main()
{
Dog TheDog;
Cat TheCat;

React( TheDog );
React( TheCat );
}

Which MakeNoise() function is called in React(). Just by looking at
the function, nobody can tell. It depends on the exact runtime
type of Pet (Note that Pet is a *reference* to an Animal. Polymorphism
works only with references or pointers). If a Dog object is passed to
React(), then Dog::MakeNoise() is called. If a Cat object is passed to
React(), then Cat::MakeNoise() is called. The decission which function
to call is delayed until runtime, when the exact runtime type of the
object is known.
Also note: The important syntactic element is the keyword 'virtual' in
the class declarations. It is this keyword, which switches to this
behaviour. Without it Animal::MakeNoise() would be called in both
cases.
 
P

prashna

No. In principle you got it.
Polymorphism means that the decision on which function to actually
call is delayed until runtime when the exact type of the object
to use for the call can be determined.
Thanks Buchegger,
Just one more question..Because the function to be invoked is decided
at runtime for function pointers, can we say C supports late
binding?Or is it that late binding(Like polymorphism) also is
associated with objects?
 
K

Karl Heinz Buchegger

prashna said:
Thanks Buchegger,
Just one more question..Because the function to be invoked is decided
at runtime for function pointers, can we say C supports late
binding?

Personally I wouldn't call it 'late binding'. In C it's just
an indirect function call through a variable, with you (the programmer)
have to take care of the details.
 

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

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top