423,851 Members | 2,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

static vs dynamic dispatch

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.