No video

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

  Рет қаралды 16,354

Mike Shah

Mike Shah

2 жыл бұрын

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

Пікірлер: 37
@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!
@nevis2769
@nevis2769 2 жыл бұрын
Great video! Thanks Mike.
@MikeShah
@MikeShah 2 жыл бұрын
You are most welcome!
@shivampanjiyara8288
@shivampanjiyara8288 3 ай бұрын
thanx bro . love y0ou=
@heretoinfinity9300
@heretoinfinity9300 7 ай бұрын
What's the link with the multiple inheritance vtables mentioned at the end?
@MikeShah
@MikeShah 7 ай бұрын
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 10 ай бұрын
kudos for your hardwork. But it's difficult to understand, can you explain again with good diagrams?
@MikeShah
@MikeShah 10 ай бұрын
Cheers! See around 2 minute mark for some pictures
@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 11 ай бұрын
@@AlberTesla1024 I believe that is true
@mouseminer2978
@mouseminer2978 8 ай бұрын
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
@el_joker_2243
@el_joker_2243 11 ай бұрын
thanks alot
@MikeShah
@MikeShah 11 ай бұрын
Cheers!
@writecompiler
@writecompiler Жыл бұрын
nice
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@antonfernando8409
@antonfernando8409 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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.
@antonfernando8409
@antonfernando8409 Жыл бұрын
cool
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@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.
@zizicz1661
@zizicz1661 4 ай бұрын
Is that vim please? I need to settle smth
@zizicz1661
@zizicz1661 4 ай бұрын
sorry it's late and i didn't notice, kudos to you for that
@MikeShah
@MikeShah 4 ай бұрын
Cheers! @@zizicz1661 (This might be relevant as well! kzfaq.info/get/bejne/m5p1adGC1cqbiKc.html)
@ILoveSoImAlive
@ILoveSoImAlive 9 ай бұрын
2 ведра воды о полиморфизме
@RAHULTMNT100
@RAHULTMNT100 Жыл бұрын
too slow
@snapman218
@snapman218 Жыл бұрын
Dude, use a slide show or something rather than writing.
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 34 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 16 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 3,5 МЛН
Ditch your Favorite Programming Paradigm
6:08
Code Persist
Рет қаралды 175 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 778 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 838 М.
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН