473,289 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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 12019
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
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
4
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,...
0
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...
13
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...
7
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
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++"...
4
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...
1
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. ...
3
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.