473,513 Members | 2,533 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 12030
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.comwrote:
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.comwrote:
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.comwrote:
>>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
4771
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? ...
0
1761
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...
13
14590
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...
7
8204
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
3765
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...
4
2701
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...
1
1139
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
2339
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
7270
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...
0
7178
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...
0
7397
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. ...
0
7565
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...
0
5704
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...
0
3255
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...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
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...

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.