static vs dynamic dispatch

M

markww

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
 
X

Xeranic

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).
 
A

Alan Johnson

markww said:
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.
 
M

markww

Alan said:
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.

Thanks Alan,

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top