What happens to VTABLE when base class has virtual function and derived class does?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhi97
    New Member
    • Jul 2010
    • 2

    #1

    What happens to VTABLE when base class has virtual function and derived class does?

    If derived class will be having vtable then why we need vtable for derived class?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The vtable identifies what function to call when you call a virtual function on an object.

    Every class has a vtable if that class has a virtual function.

    In your case, base objects have a vtable that will call the base class version of the virtual function. Derived objects will have a vtable that calls the derived class version of the virtual function.

    If there is no derived class version of the virtual function, then the derived class vtable will call the base class version of the virtual function.

    Comment

    • abhi97
      New Member
      • Jul 2010
      • 2

      #3
      ok. my question is why derived class will be having the vtable. if we see in other word, derived class does not have any virtual function then why vtable?

      Comment

      • Joseph Martell
        Recognized Expert New Member
        • Jan 2010
        • 198

        #4
        If the base class does NOT have a virtual function and the derived class does NOT have a virtual funciton, then neither class gets a vtable.

        If the base class has a virtual function, then the derived class has a virtual function, whether the virtual keyword is used or not. Virtual functions don't go away after a class has been inherited.

        Comment

        Working...