virtual function and vtable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kish_nand@yahoo.com

    virtual function and vtable

    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

  • Kyle

    #2
    Re: virtual function and vtable

    kish_nand@yahoo .com wrote:[color=blue]
    > Could someone please explain me the concept behind virtual functions
    > and vtables. I am little confused about this. Please refer to the[/color]

    there is no concept of vtable in c++, its up to compiler and its 'magic'
    how virtual function are actually implemented
    [color=blue]
    > following code and tell me how many virtual tables would be created and
    > what they would contain:
    >[/color]

    #include <iostream>
    using std::cout;
    [color=blue]
    > class base
    > {[/color]
    public:[color=blue]
    > virtual void display()
    > {
    > cout<<"base display"<<endl;
    > }
    > void disp()
    > {
    > cout<<"base disp"<<endl;
    > }[/color]

    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
    [color=blue]
    > };
    >
    > class d1: public base
    > {[/color]
    public:[color=blue]
    > void display()
    > {
    > cout<<"d1 display"<<endl;
    > }
    > };
    >
    > class d2: public base
    > {[/color]
    public:[color=blue]
    > void display()
    > {
    > cout<<"d2 display"<<endl;
    > }
    > };
    >[/color]

    //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()[color=blue]
    > {
    > base *b = new d1;[/color]

    //> 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[color=blue]
    > }
    >
    > Thanking you,
    >
    > regards
    >
    > Nand Kishore
    >[/color]

    Comment

    • kish_nand@yahoo.com

      #3
      Re: virtual function and vtable

      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.

      Comment

      • Bangalore

        #4
        Re: virtual function and vtable

        > kish_nand@yahoo .comwrote:
        Could someone please explain me the concept behind virtual functions[color=blue]
        > and vtables.[/color]
        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
        [color=blue]
        > I am little confused about this. Please refer to the
        > following code and tell me how many virtual tables would be created[/color]
        and[color=blue]
        > what they would contain:[/color]

        Always , there will be only one vtable per class and these vtable
        contain the addresses of virtual functions.
        [color=blue]
        >
        > 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[/color]

        Comment

        • kish_nand@yahoo.com

          #5
          Re: virtual function and vtable

          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.

          Comment

          • Kyle

            #6
            Re: virtual function and vtable

            kish_nand@yahoo .com wrote:[color=blue]
            > 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.[/color]

            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



            and if you just wanted to understand vtable mechanism im sorry, i merely
            corrected your code to get expected behaviour

            Comment

            • Shezan Baig

              #7
              Re: virtual function and vtable

              kish_nand@yahoo .com wrote:[color=blue]
              > So, will there be 3 vtables for the above code[/color]


              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-

              Comment

              • kish_nand@yahoo.com

                #8
                Re: virtual function and vtable

                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.

                Comment

                • benben

                  #9
                  Re: virtual function and vtable

                  >I was talking about the vtable which are used to resolve the function[color=blue]
                  > calls at runtime and not of virtual destructor.[/color]

                  A good resource is Stan Lippman's Inside the C++ Object Model.
                  [color=blue]
                  >void main() is also
                  > legal and its absolutely fine to have void main, the compiler won't
                  > give you any error.
                  >[/color]

                  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


                  Comment

                  • Scott J. McCaughrin

                    #10
                    Re: virtual function and vtable

                    Kyle <invalid@e.mail > wrote:
                    : kish_nand@yahoo .com 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.

                    Comment

                    Working...