I am confused with VTable ;-)

Discussion in 'C++' started by Pani, Jul 23, 2003.

  1. Pani

    Pani Guest

    Hi
    I am new to C++ ,i really dont understand why there is a need for
    VTable and can some experts give an example where exactly and how a compiler
    uses it

    Regards
    Pani
    Pani, Jul 23, 2003
    #1
    1. Advertising

  2. "Pani" <> wrote in message
    news:bflj8v$k3i$...
    > Hi
    > I am new to C++ ,i really dont understand why there is a need for
    > VTable and can some experts give an example where exactly and how a

    compiler
    > uses it
    >
    > Regards
    > Pani
    >


    Caution, this example may imply certain views about the theory of evolution
    for which the author is completely irresponsible.

    #include <iostream>
    using namespace std;

    class Ape
    {
    public:
    virtual void what_am_i() { cout << "i'm an ape\n"; }
    };

    class Human : public Ape
    {
    public:
    virtual void what_am_i() { cout << "i'm an human\n"; }
    };

    int main()
    {
    Ape* ape1 = new Ape();
    Ape* ape2 = new Human();
    ape1->what_am_i();
    ape2->what_am_i();
    }

    Would you want to be mistaken for an ape?

    Although ape2 has type Ape*, it is pointing to a Human object and therefore
    a Human vtable. So the compiler uses the Human vtable to call
    Human::what_am_i().

    john
    John Harrison, Jul 23, 2003
    #2
    1. Advertising

  3. Pani wrote:
    >
    > Hi
    > I am new to C++ ,i really dont understand why there is a need for
    > VTable and can some experts give an example where exactly and how a compiler
    > uses it


    Try <http://www.codesourcery.com/cxx-abi/abi.html#vtable>.

    regards,
    alexander.
    Alexander Terekhov, Jul 23, 2003
    #3
    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. Daniel Heiserer
    Replies:
    1
    Views:
    2,895
    Artie Gold
    Aug 1, 2003
  2. r
    Replies:
    2
    Views:
    689
    Rob Williscroft
    Aug 2, 2003
  3. sks_cpp

    type_info, vtable

    sks_cpp, Aug 19, 2003, in forum: C++
    Replies:
    54
    Views:
    2,224
    Ron Natalie
    Aug 27, 2003
  4. Quansheng Liang
    Replies:
    2
    Views:
    552
    Quansheng Liang
    May 25, 2004
  5. Paul
    Replies:
    1
    Views:
    635
    Marco Wahl
    Jun 24, 2004
Loading...

Share This Page