Classes part 18 - Understanding the vtable (Popular interview question) | Modern Cpp Series Ep. 54

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • ►Full C++ Series Playlist: • The C++ Programming La...
    ►Find full courses on: courses.mshah.io/
    ►Join as Member to Support the channel: / @mikeshah
    ►Lesson Description: In this lesson I show you what exactly the vtable is in C++. The vtable is the data structure which holds information about which member functions to call for an instance of class when the presence of 'virtual' exists in inheritance. Understanding a little bit about this data structure, will also give you some intuition about a small performance cost (i.e. storage for the pointer, and following a pointer to call a function) about virtual functions as well.
    ►TH-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!

ความคิดเห็น • 39

  • @Gaspardkirira
    @Gaspardkirira 12 วันที่ผ่านมา +2

    you are a best teacher

    • @MikeShah
      @MikeShah  12 วันที่ผ่านมา

      @@Gaspardkirira Thank you for the kind words 🙏

  • @dhanushs1802
    @dhanushs1802 2 ปีที่แล้ว +5

    Great video. Very neatly explained as always. Thank you.

  • @jugalshah8199
    @jugalshah8199 2 ปีที่แล้ว +4

    Thank you i waited for this topic for long its crystal clear now.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      You are most welcome!

  • @nevis2769
    @nevis2769 2 ปีที่แล้ว +3

    Great video! Thanks Mike.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      You are most welcome!

  • @shivampanjiyara8288
    @shivampanjiyara8288 4 หลายเดือนก่อน +1

    thanx bro . love y0ou=

  • @tejasbachhav3921
    @tejasbachhav3921 ปีที่แล้ว +2

    kudos for your hardwork. But it's difficult to understand, can you explain again with good diagrams?

    • @MikeShah
      @MikeShah  11 หลายเดือนก่อน

      Cheers! See around 2 minute mark for some pictures

  • @el_joker_2243
    @el_joker_2243 ปีที่แล้ว +1

    thanks alot

  • @heretoinfinity9300
    @heretoinfinity9300 8 หลายเดือนก่อน +1

    What's the link with the multiple inheritance vtables mentioned at the end?

    • @MikeShah
      @MikeShah  8 หลายเดือนก่อน

      I'm not 100% sure on implementation, but you could have multiple 'vtables' for a derived object pointing to different member functions in the derived class (that's likely how this is implemented at least in langauges that allow for multiple interface inheritance) -- perhaps a more clever implementation of multiple inheritance builds a single table with the derived member functions however.

  • @grenadier4702
    @grenadier4702 ปีที่แล้ว +1

    If I don't override a base function, is vtable still created for my derived class? Or only the pointer is inherited and points straight to the base vtable? For instance:
    class Base {
    public:
    virtual void swim() {}
    }
    class Derived: public Base {}
    Derived derived;

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      As soon as you introduce 'virtual' or 'override' in a class a vtable (or some mechanism to achieve polymorphism) is created. In this instance, If you make a call to 'swim' from the derived class, then we'll be looking into Base's vtable to call the appropriate swim. So while it's probably implementation defined (i.e. up to the compiler), I would assume this example generates a vtable.

    • @grenadier4702
      @grenadier4702 ปีที่แล้ว +1

      @@MikeShah Thanks! :)

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      @@grenadier4702 Cheers!

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      @@AlberTesla1024 I believe that is true

  • @kunalkumar-ic1dc
    @kunalkumar-ic1dc ปีที่แล้ว +1

    virtual pointer and virtual table are part of class. So what is the type of virtual pointer and virtual table?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      I believe the virtual table is of type void** (a pointer to a table of pointers)

    • @kunalkumar-ic1dc
      @kunalkumar-ic1dc ปีที่แล้ว +1

      ​@@MikeShahMay I know, why void?

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      @@kunalkumar-ic1dc It's merely holding an address, no 'type' associated with it.

    • @kunalkumar-ic1dc
      @kunalkumar-ic1dc ปีที่แล้ว +1

      @@MikeShah Thanks for the clarification.

  • @writecompiler
    @writecompiler ปีที่แล้ว +1

    nice

  • @zizicz1661
    @zizicz1661 5 หลายเดือนก่อน +1

    Is that vim please? I need to settle smth

    • @zizicz1661
      @zizicz1661 5 หลายเดือนก่อน +1

      sorry it's late and i didn't notice, kudos to you for that

    • @MikeShah
      @MikeShah  5 หลายเดือนก่อน

      Cheers! @@zizicz1661 (This might be relevant as well! th-cam.com/video/e4E6nQpd7Xs/w-d-xo.html)

  • @antonfernando8409
    @antonfernando8409 2 ปีที่แล้ว

    why destuctors have to be virtual and constructors can't be virtual, I was asked this once in an interviews, still not sure I have realize an answer.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      The way I would probably answer this is that we need an object to be fully constructed (including its vtable for later redirection) before we can do anything with it. Probably best to hear it from Bjarne himself www.stroustrup.com/bs_faq2.html#virtual-ctor

    • @antonfernando8409
      @antonfernando8409 2 ปีที่แล้ว

      @@MikeShah cool thanks.

    • @capetzproductions8690
      @capetzproductions8690 ปีที่แล้ว +1

      Virtual functions work based on the vtable mechanism, which is set up after the constructor finishes executing.Moreover, there's no need for constructors to be virtual. When an object is created, the constructor of the specific class is known at compile time, and there's no ambiguity about which constructor to call.

  • @antonfernando8409
    @antonfernando8409 2 ปีที่แล้ว

    cool

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +1

      Cheers!

  • @ILoveSoImAlive
    @ILoveSoImAlive 11 หลายเดือนก่อน

    2 ведра воды о полиморфизме

  • @RAHULTMNT100
    @RAHULTMNT100 ปีที่แล้ว

    too slow

  • @snapman218
    @snapman218 ปีที่แล้ว +2

    Dude, use a slide show or something rather than writing.

  • @mouseminer2978
    @mouseminer2978 9 หลายเดือนก่อน

    Hi, I am strugling with this vtable error. Please help
    Below is the arduino code for ePaper display.
    #include
    #include //2.13" E-paper Module (White/Red/Black)
    #include
    #include
    #include
    GxIO_Class io(SPI, SS, 17, 16);
    GxEPD_Class display(io, 16, 4);
    void setup() {
    }
    void loop() {
    }
    Here is the error below
    undefined reference to `vtable for GxIO'
    collect2.exe: error: ld returned 1 exit status
    exit status 1
    Compilation error: exit status 1