473,624 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very basic question on Dynamic binding?

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("H IIIIII\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..anybod y please help me..
Jul 22 '05 #1
3 1627
prashna wrote:

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("H IIIIII\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::MakeNoi se() would be called in both
cases.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
> 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..Becau se 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?
Jul 22 '05 #3
prashna wrote:
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..Becau se 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.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3411
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
30
2710
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
1
7562
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
3
5521
by: compgeek.320 | last post by:
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??
17
1624
by: hanumizzle | last post by:
I have used Perl for a long time, but I am something of an experimental person and mean to try something new. Most of my 'work' with Vector Linux entails the use of Perl (a bit of a misnomer as it is not now a paid position -- I am not yet even out of K-12), and there a lot of things I love about it. I can look past a number of misfeatures in Perl, but I am surprised to see that Python has very few (that I know of). Most of them are...
7
10740
by: Joey | last post by:
I don't care what .net language this come in but I really need to determine if a disk is dynamic or basic. I have posted something in the WMI group but no one knows how to do it. Does anyone have any idea how I can determine this? At this point I don't care if it is a third plugin freeware app I use that writes it to a text file that I could read out. Thanks!
13
1683
by: Jess | last post by:
Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{
2
9666
by: 09876 | last post by:
Hi: all I understand the difference between dynamic binding and static binding. But I just wonder what is the point to make the distinction between the dynamic binding and static binding. For example, in c, if a function pointer is used. say something like that float (*pt2Func)(float, float); so the compiler doesn't know what is pt2Func to be called until at run-time. Is it dynamic binding? Thanks.
26
2798
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8174
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.