473,785 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static vs dynamic dispatch

Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks

Dec 18 '06 #1
5 12050
The static and dynamic means the time to dispatch. The static dispatch
be determined at compile time, dynamic at runtime. In C++ dynamic
dispatch is implemented by using virtual function (or pointer to
function as c style).

On Dec 18, 8:13 am, "markww" <mar...@gmail.c omwrote:
Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks
Dec 18 '06 #2
Hi Xeranic,

But what is 'dispatching'? What is being dispatched?

Thanks

Xeranic wrote:
The static and dynamic means the time to dispatch. The static dispatch
be determined at compile time, dynamic at runtime. In C++ dynamic
dispatch is implemented by using virtual function (or pointer to
function as c style).

On Dec 18, 8:13 am, "markww" <mar...@gmail.c omwrote:
Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks
Dec 18 '06 #3
* markww:
Hi Xeranic,

But what is 'dispatching'? What is being dispatched?

Thanks

Xeranic wrote:
>The static and dynamic means the time to dispatch. The static dispatch
be determined at compile time, dynamic at runtime. In C++ dynamic
Please don't top-post.
>dispatch is implemented by using virtual function (or pointer to
function as c style).

On Dec 18, 8:13 am, "markww" <mar...@gmail.c omwrote:
>>Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 18 '06 #4
markww wrote:
Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks
In the context of C++, "dispatch" is just a fancy name for calling a
member function. A "static" dispatch is one where all the type
information needed to decide which function to call is known at compile
time. Consider the following example:

#include <iostream>

class A
{
public:
void f() const { std::cout << "A::f()" << std::endl ; }
} ;

class B
{
public:
void f() const { std::cout << "B::f()" << std::endl ; }
} ;

int main()
{
A a ;
B b ;

a.f() ;
b.f() ;
}

Here, the compiler has all the type information needed to figure out
which function "f" to call in each case. In the first call, A::f is
called, and in the second, B::f is called.

Dynamic dispatching is needed when it cannot be determined until runtime
which function to call. In C++, this is implemented using virtual
functions. Consider the following:

#include <iostream>

class base
{
public:
virtual void f() const = 0 ;
virtual ~base() {}
} ;

class A : public base
{
public:
virtual void f() const { std::cout << "A::f()" << std::endl ; }
} ;

class B : public base
{
public:
virtual void f() const { std::cout << "B::f()" << std::endl ; }
} ;

void dispatch(const base & x)
{
x.f() ;
}

int main()
{
A a ;
B b ;

dispatch(a) ;
dispatch(b) ;
}

The line "x.f() ;" gets executed twice, but a different function gets
called each time.

--
Alan Johnson
Dec 18 '06 #5

Alan Johnson wrote:
markww wrote:
Hi,

Can someone explain to me what static and dynamic dispatch are and what
the difference is between the two? Do this even occurr in C++?

Thanks

In the context of C++, "dispatch" is just a fancy name for calling a
member function. A "static" dispatch is one where all the type
information needed to decide which function to call is known at compile
time. Consider the following example:

#include <iostream>

class A
{
public:
void f() const { std::cout << "A::f()" << std::endl ; }
} ;

class B
{
public:
void f() const { std::cout << "B::f()" << std::endl ; }
} ;

int main()
{
A a ;
B b ;

a.f() ;
b.f() ;
}

Here, the compiler has all the type information needed to figure out
which function "f" to call in each case. In the first call, A::f is
called, and in the second, B::f is called.

Dynamic dispatching is needed when it cannot be determined until runtime
which function to call. In C++, this is implemented using virtual
functions. Consider the following:

#include <iostream>

class base
{
public:
virtual void f() const = 0 ;
virtual ~base() {}
} ;

class A : public base
{
public:
virtual void f() const { std::cout << "A::f()" << std::endl ; }
} ;

class B : public base
{
public:
virtual void f() const { std::cout << "B::f()" << std::endl ; }
} ;

void dispatch(const base & x)
{
x.f() ;
}

int main()
{
A a ;
B b ;

dispatch(a) ;
dispatch(b) ;
}

The line "x.f() ;" gets executed twice, but a different function gets
called each time.

--
Alan Johnson
Thanks Alan,

Mark

Dec 18 '06 #6

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

Similar topics

5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
4
4789
by: jrefactors | last post by:
I want to distinguish between static SQL, dynamic SQL, and embedded SQL, but couldn't find too much useful resources in the web. For example, if we put SQL statements (SELECT, INSERT, UPDATE, etc...) inside an application (e.g. Java application, VB application, etc...), do we consider those SQL statements as static SQL? or embedded SQL? How about dynamic SQL? any practical examples? Please advise. thanks!!
0
1783
by: todddeluca | last post by:
I am posting code for calling almost any python function from php, because it seems generally useful. Please feel free to suggest improvements or tell me this has already been done better somewhere else, etc. My limited searching turned up nothing. I work in a heterogeneous environment with php web pages and python modules/scripts. This code requires no no creation of an ad hoc command line interface to the python module and/or ad hoc...
13
14622
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
7
8224
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
3
3776
by: Tigera | last post by:
Greetings, I too have succumbed to the perhaps foolish urge to write a video game, and I have been struggling with the implementation of multiple dispatch. I read through "More Effective C++" by Scott Meyers, and though I am impressed with his implementation, I wanted to find a way to use multiple dispatch in a way that was less complicated. Forgive me if the result, below, has been posted before or written of before - I haven't read...
4
2712
by: beatTheDevil | last post by:
I searched, I swear. All my keywords were ignored by the search engine. Or I may just suck at searching with the right keywords, but I've been unable to find information about this. Perhaps somebody can elaborate. I would to like to create a function, included in the header of an HTML page, which calls functions which are defined later, in <script /> tags farther down in the body of the page (I understand this is a terrible idea, but it is a...
1
1152
by: slink9 | last post by:
I have VS2005 Professional with VWD and have a need to develop static as well as dynamic web sites. I am trying to modify my existing static web site which can be easily done in Expressions Web. Can I accomplish this just as easily using tools in VS2005? How? Would VS2008 be better?
3
2351
by: puzzlecracker | last post by:
Is it possible to package a template implementation class into a library and how it is done (say into a dll or a static one). Since template expansion in c++ is static and takes place at compile time (unlike csharp), I would like to find out how we can simulate that? Thank
0
9647
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
10357
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
10163
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
10104
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
9959
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
8988
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
5397
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
4063
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
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.