By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,773 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,773 IT Pros & Developers. It's quick & easy.

Dynamic method dispatching

P: n/a
Hi,

could someone please exmaplin to me what dynamic method dispatching is
and how it's connected to virtual methods? Thanks

Terry

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Terry" <te***@nospam.com> wrote in message
news:3f********@clarion.carno.net.au...
Hi,

could someone please exmaplin to me what dynamic method dispatching is
and how it's connected to virtual methods? Thanks

Terry


Virtual methods use dynamic dispatching. This means that the method that is
called depends on the type at run time, not the type at compile time.

E.g.

class B
{
public:
virtual void func() { cout << "its B" }
};

class D : public B
{
public:
virtual void func() { cout << "its D" }
};

B* b_ptr = new D();
b_ptr->func();

Which func is called, B::func or D::func? At compile time b_ptr is a B* so
you could argue that B::func is called (static dispatching). But when the
program runs b_ptr happens to be pointing to a D, so you could argue that
D::func is called (dynamic dispatching).

The answer in this case is the D::func is called. Because func is virtual
dynamic dispatching is used. If func where not virtual then B::func would be
called.

Why don't you try it and see? Then remove virtual and try again.

john
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.