Тёмный

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

Mike Shah
Подписаться 23 тыс.
Просмотров 18 тыс.
50% 1

►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.
►RU-vid Channel: / mikeshah
►Please like and subscribe to help the channel!

Опубликовано:

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 39   
@Gaspardkirira
@Gaspardkirira Месяц назад
you are a best teacher
@MikeShah
@MikeShah Месяц назад
@@Gaspardkirira Thank you for the kind words 🙏
@dhanushs1802
@dhanushs1802 2 года назад
Great video. Very neatly explained as always. Thank you.
@jugalshah8199
@jugalshah8199 2 года назад
Thank you i waited for this topic for long its crystal clear now.
@MikeShah
@MikeShah 2 года назад
You are most welcome!
@heretoinfinity9300
@heretoinfinity9300 9 месяцев назад
What's the link with the multiple inheritance vtables mentioned at the end?
@MikeShah
@MikeShah 9 месяцев назад
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.
@tejasbachhav3921
@tejasbachhav3921 Год назад
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
@nevis2769
@nevis2769 2 года назад
Great video! Thanks Mike.
@MikeShah
@MikeShah 2 года назад
You are most welcome!
@grenadier4702
@grenadier4702 Год назад
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 Год назад
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 Год назад
@@MikeShah Thanks! :)
@MikeShah
@MikeShah Год назад
@@grenadier4702 Cheers!
@MikeShah
@MikeShah Год назад
@@AlberTesla1024 I believe that is true
@kunalkumar-ic1dc
@kunalkumar-ic1dc Год назад
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 Год назад
​@@MikeShahMay I know, why void?
@MikeShah
@MikeShah Год назад
@@kunalkumar-ic1dc It's merely holding an address, no 'type' associated with it.
@kunalkumar-ic1dc
@kunalkumar-ic1dc Год назад
@@MikeShah Thanks for the clarification.
@shivampanjiyara8288
@shivampanjiyara8288 5 месяцев назад
thanx bro . love y0ou=
@zizicz1661
@zizicz1661 6 месяцев назад
Is that vim please? I need to settle smth
@zizicz1661
@zizicz1661 6 месяцев назад
sorry it's late and i didn't notice, kudos to you for that
@MikeShah
@MikeShah 6 месяцев назад
Cheers! @@zizicz1661 (This might be relevant as well! ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-e4E6nQpd7Xs.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 года назад
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 Год назад
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.
@el_joker_2243
@el_joker_2243 Год назад
thanks alot
@MikeShah
@MikeShah Год назад
Cheers!
@writecompiler
@writecompiler Год назад
nice
@MikeShah
@MikeShah Год назад
Cheers!
@antonfernando8409
@antonfernando8409 2 года назад
cool
@MikeShah
@MikeShah 2 года назад
Cheers!
@ILoveSoImAlive
@ILoveSoImAlive Год назад
2 ведра воды о полиморфизме
@RAHULTMNT100
@RAHULTMNT100 Год назад
too slow
@snapman218
@snapman218 Год назад
Dude, use a slide show or something rather than writing.
@mouseminer2978
@mouseminer2978 10 месяцев назад
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
Далее
are "smart pointers" actually smart?
9:44
Просмотров 76 тыс.
Rust: When C Code Isn't Enough
8:26
Просмотров 170 тыс.
Move Semantics in C++
13:10
Просмотров 296 тыс.