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

Поділитися
Вставка
  • Опубліковано 4 гру 2024

КОМЕНТАРІ • 39

  • @Gaspardkirira
    @Gaspardkirira 2 місяці тому +2

    you are a best teacher

    • @MikeShah
      @MikeShah  2 місяці тому

      @@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!

  • @heretoinfinity9300
    @heretoinfinity9300 11 місяців тому +1

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

    • @MikeShah
      @MikeShah  11 місяців тому

      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.

  • @shivampanjiyara8288
    @shivampanjiyara8288 7 місяців тому +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  Рік тому

      Cheers! See around 2 minute mark for some pictures

  • @grenadier4702
    @grenadier4702 2 роки тому +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  2 роки тому +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 2 роки тому +1

      @@MikeShah Thanks! :)

    • @MikeShah
      @MikeShah  2 роки тому

      @@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.

  • @el_joker_2243
    @el_joker_2243 Рік тому +1

    thanks alot

  • @zizicz1661
    @zizicz1661 8 місяців тому +1

    Is that vim please? I need to settle smth

    • @zizicz1661
      @zizicz1661 8 місяців тому +1

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

    • @MikeShah
      @MikeShah  8 місяців тому

      Cheers! @@zizicz1661 (This might be relevant as well! ua-cam.com/video/e4E6nQpd7Xs/v-deo.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.

  • @writecompiler
    @writecompiler Рік тому +1

    nice

  • @antonfernando8409
    @antonfernando8409 2 роки тому

    cool

  • @ILoveSoImAlive
    @ILoveSoImAlive Рік тому

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

  • @RAHULTMNT100
    @RAHULTMNT100 Рік тому

    too slow

  • @snapman218
    @snapman218 Рік тому +3

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

  • @mouseminer2978
    @mouseminer2978 Рік тому

    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