calling virtual function from base class

Discussion in 'C++' started by Phui Hock, Sep 7, 2004.

  1. Phui  Hock

    Phui Hock Guest

    "When you make a virtual function call through a base-class pointer
    (that is, when you make a polymorphic call), the compiler quietly
    inserts code to fetch the VPTR and look up the function address in the
    VTABLE..."

    Can anyone tell me how the VPTR of the derived class is fetched? Where
    can I get more information bout this?

    Thank u
    Phui Hock, Sep 7, 2004
    #1
    1. Advertising

  2. "Phui Hock" <> wrote in message
    news:chkseq$...
    > "When you make a virtual function call through a base-class pointer
    > (that is, when you make a polymorphic call), the compiler quietly
    > inserts code to fetch the VPTR and look up the function address in the
    > VTABLE..."
    >
    > Can anyone tell me how the VPTR of the derived class is fetched? Where
    > can I get more information bout this?
    >
    > Thank u
    >


    'Inside the C++ Object Model' by Stanley Lippman.

    john
    John Harrison, Sep 7, 2004
    #2
    1. Advertising

  3. Phui  Hock

    Mike Wahler Guest

    "Phui Hock" <> wrote in message
    news:chkseq$...
    > "When you make a virtual function call through a base-class pointer
    > (that is, when you make a polymorphic call), the compiler quietly
    > inserts code to fetch the VPTR and look up the function address in the
    > VTABLE..."
    >
    > Can anyone tell me how the VPTR of the derived class is fetched? Where
    > can I get more information bout this?


    The book John cited does indeed give useful insight into
    the design of C++.

    But note that the actual mechanics of e.g. polymorphism is
    completely implementation-specific. Each might implement it
    in its own way. The only language requirement is that a
    particular *behavior* is the result. The language does not
    define things such as 'v-tables' or 'v-pointers'.

    -Mike
    Mike Wahler, Sep 7, 2004
    #3
  4. Phui Hock wrote:

    > "When you make a virtual function call through a base-class pointer
    > (that is, when you make a polymorphic call),
    > the compiler quietly inserts code
    > to fetch the VPTR and look up the function address in the VTABLE..."
    >
    > Can anyone tell me how the VPTR of the derived class is fetched?
    > Where can I get more information bout this?


    The VPTR *is* a pointer to the VTABLE for the derived class
    if the object was created using a constructor for the derived class.
    E. Robert Tisdale, Sep 7, 2004
    #4
  5. Phui  Hock

    JKop Guest

    Phui Hock posted:

    > "When you make a virtual function call through a base-class pointer
    > (that is, when you make a polymorphic call), the compiler quietly
    > inserts code to fetch the VPTR and look up the function address in the
    > VTABLE..."
    >
    > Can anyone tell me how the VPTR of the derived class is fetched? Where
    > can I get more information bout this?
    >
    > Thank u


    One thing I think you should realize: If you have a scenario where you have
    a certain function:

    void EatPoo()

    And this function is NON-virtual in the base class:

    class Base
    {
    public:

    void EatPoo()
    {
    cout << "Base::EatPoo";
    }
    };

    And then you have a derived class as so:

    class Derived : public Base
    {
    public:

    virtual void EatPoo()
    {
    cout << "Derived::EatPoo";
    }
    };

    That now when you do the following:

    int main()
    {
    Derived k;

    Base& r = k;

    r.EatPoo();
    }

    It's "Base::EatPoo" that will be called.


    -JKop
    JKop, Sep 7, 2004
    #5
    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.

Share This Page