Dynamic method dispatching

Discussion in 'C++' started by Terry, Aug 31, 2003.

  1. Terry

    Terry Guest

    Hi,

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

    Terry
     
    Terry, Aug 31, 2003
    #1
    1. Advertising

  2. "Terry" <> wrote in message
    news:3f51a8da$...
    > 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
     
    John Harrison, Aug 31, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sapsi
    Replies:
    0
    Views:
    350
    sapsi
    Jun 26, 2007
  2. Evan Webb
    Replies:
    0
    Views:
    113
    Evan Webb
    Jan 13, 2004
  3. trans.  (T. Onoma)

    Method Dispatching

    trans. (T. Onoma), Jan 13, 2005, in forum: Ruby
    Replies:
    2
    Views:
    97
    trans. (T. Onoma)
    Jan 14, 2005
  4. Francis Hwang
    Replies:
    15
    Views:
    226
    Francis Hwang
    Jul 9, 2005
  5. Esteban Manchado Velázquez

    [SWIG] Dynamic method dispatching with "send"

    Esteban Manchado Velázquez, Sep 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    123
    Esteban Manchado Velázquez
    Sep 3, 2005
Loading...

Share This Page