virtual function and vtable

Discussion in 'C++' started by kish_nand@yahoo.com, Oct 3, 2005.

  1. Guest

    Could someone please explain me the concept behind virtual functions
    and vtables. I am little confused about this. Please refer to the
    following code and tell me how many virtual tables would be created and
    what they would contain:

    class base
    {
    virtual void display()
    {
    cout<<"base display"<<endl;
    }
    void disp()
    {
    cout<<"base disp"<<endl;
    }
    };

    class d1: public base
    {
    void display()
    {
    cout<<"d1 display"<<endl;
    }
    };

    class d2: public base
    {
    void display()
    {
    cout<<"d2 display"<<endl;
    }
    };

    void main()
    {
    base *b = new d1;
    d1->display();
    }

    Thanking you,

    regards

    Nand Kishore
    , Oct 3, 2005
    #1
    1. Advertising

  2. Kyle Guest

    wrote:
    > Could someone please explain me the concept behind virtual functions
    > and vtables. I am little confused about this. Please refer to the


    there is no concept of vtable in c++, its up to compiler and its 'magic'
    how virtual function are actually implemented

    > following code and tell me how many virtual tables would be created and
    > what they would contain:
    >


    #include <iostream>
    using std::cout;

    > class base
    > {

    public:
    > virtual void display()
    > {
    > cout<<"base display"<<endl;
    > }
    > void disp()
    > {
    > cout<<"base disp"<<endl;
    > }


    virtual ~base() {}

    //you want vitrual destructor here - if you delete pointer of type base*
    which points to object of class derived from base, destructor of class
    base would be called only if you wont provide virtual destructor in base

    > };
    >
    > class d1: public base
    > {

    public:
    > void display()
    > {
    > cout<<"d1 display"<<endl;
    > }
    > };
    >
    > class d2: public base
    > {

    public:
    > void display()
    > {
    > cout<<"d2 display"<<endl;
    > }
    > };
    >


    //by the way, using struct instead of class would save you writing
    'public' everywhere in your example

    //> void main()
    //you cant have void main(), its always 'int main'

    int main()
    > {
    > base *b = new d1;


    //> d1->display();
    //d1 is a type, you probably wanted
    b->display();

    delete b;
    //event though main do return int you dont have to return it explicitly
    as 'return 0;' is assumed
    > }
    >
    > Thanking you,
    >
    > regards
    >
    > Nand Kishore
    >
    Kyle, Oct 3, 2005
    #2
    1. Advertising

  3. Guest

    I was talking about the vtable which are used to resolve the function
    calls at runtime and not of virtual destructor. void main() is also
    legal and its absolutely fine to have void main, the compiler won't
    give you any error.
    , Oct 3, 2005
    #3
  4. Bangalore Guest

    > rote:
    Could someone please explain me the concept behind virtual functions
    > and vtables.

    Virtual functions are meant for dynamic binding.
    As per my understanding , when we declare a function
    as virtual , compiler will create vtable per class. vtable consists
    addresses
    of these virtual functions.
    For the below program,
    vtable for class base contains only address for display().

    In the d1 class , vtable contains address for d1::display()
    if this funtion is not overloaded in the derived class , then
    content of vtable would be base::display()

    Same things happen in d2 class.

    I also have least idea about ,how compiler really resolves address of
    these
    virtual functions at run time

    > I am little confused about this. Please refer to the
    > following code and tell me how many virtual tables would be created

    and
    > what they would contain:


    Always , there will be only one vtable per class and these vtable
    contain the addresses of virtual functions.

    >
    > class base
    > {
    > virtual void display()
    > {
    > cout<<"base display"<<endl;
    > }
    > void disp()
    > {
    > cout<<"base disp"<<endl;
    > }
    > };
    >
    > class d1: public base
    > {
    > void display()
    > {
    > cout<<"d1 display"<<endl;
    > }
    > };
    >
    > class d2: public base
    > {
    > void display()
    > {
    > cout<<"d2 display"<<endl;
    > }
    > };
    >
    > void main()
    > {
    > base *b = new d1;
    > d1->display();
    > }
    >
    > Thanking you,
    >
    > regards
    >
    > Nand Kishore
    Bangalore, Oct 3, 2005
    #4
  5. Guest

    Thanks for your reply.

    So, will there be 3 vtables for the above code, one for base, one for
    d1 and one for d2. Or just one for the base class.
    , Oct 3, 2005
    #5
  6. Kyle Guest

    wrote:
    > I was talking about the vtable which are used to resolve the function
    > calls at runtime and not of virtual destructor. void main() is also
    > legal and its absolutely fine to have void main, the compiler won't
    > give you any error.


    it just means that your compiler is broken 'void main' is illegal,
    try this compiler (one of the most standard compiliant) for compiling
    short snippets

    http://www.comeaucomputing.com/tryitout/

    and if you just wanted to understand vtable mechanism im sorry, i merely
    corrected your code to get expected behaviour
    Kyle, Oct 3, 2005
    #6
  7. Shezan Baig Guest

    wrote:
    > So, will there be 3 vtables for the above code



    Why do you need to know this? It is an implementation detail of the
    compiler. Some compilers might make 3, some compilers might make a
    hundred, some compilers might not even bother with virtual tables.

    All the C++ programmer needs to know is that when a virtual function is
    called, the most-derived override gets invoked. That's it. The rest
    is magic.

    If you really want to know how virtual tables are typically
    implemented, then try comp.compilers

    Hope this helps,
    -shez-
    Shezan Baig, Oct 3, 2005
    #7
  8. Guest

    Thanks a lot for your answer.

    I am really not very much interested what happens behind the curtain. I
    just wanted to know because some of the interviewer ask that type of
    questions, so knowing that is not harm at all. I never knew the concept
    of vtables before one interviewer asked me that. All i knew was how to
    use virtual function to implement dynamic polymorphism.
    , Oct 3, 2005
    #8
  9. benben Guest

    >I was talking about the vtable which are used to resolve the function
    > calls at runtime and not of virtual destructor.


    A good resource is Stan Lippman's Inside the C++ Object Model.

    >void main() is also
    > legal and its absolutely fine to have void main, the compiler won't
    > give you any error.
    >


    Mc's never warn you about the risk of cancer when selling you french fries.
    Compiler writers surely don't want to lose their money by giving errors to
    every program written before the formal C++ standardization.

    Ben
    benben, Oct 3, 2005
    #9
  10. Kyle <> wrote:
    : wrote:
    : > Could someone please explain me the concept behind virtual functions
    : > and vtables. I am little confused about this. Please refer to the

    : there is no concept of vtable in c++, its up to compiler and its 'magic'
    : how virtual function are actually implemented

    : > following code and tell me how many virtual tables would be created and
    : > what they would contain:
    : >

    Another poster wisely advised Lippman: his text "Inside the C++ Object
    Model" describes this issue. Ti summarize, vtables are created (by some
    compilers) on a per-class basis, so 3 vtables would result.
    Scott J. McCaughrin, Feb 9, 2006
    #10
    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. IK
    Replies:
    2
    Views:
    588
    hemraj
    Jul 23, 2004
  2. Replies:
    4
    Views:
    3,286
    Maxim Yegorushkin
    Aug 23, 2005
  3. Replies:
    5
    Views:
    699
    Ron Natalie
    Nov 20, 2006
  4. Ole Nielsby
    Replies:
    15
    Views:
    1,392
    James Kanze
    Oct 15, 2008
  5. Dave Johansen

    vtable with virtual base class?

    Dave Johansen, Dec 11, 2008, in forum: C++
    Replies:
    11
    Views:
    758
    James Kanze
    Dec 13, 2008
Loading...

Share This Page