Тёмный
No video :(

How VIRTUAL functions work in C++ 

Lukasz Lipski
Подписаться 132
Просмотров 4,8 тыс.
50% 1

In this video, I'd like to show the Virtual Method Table approach that is used to implement virtual functions in C++. This method is used by most common C++ compilers like MSVC, Clang, or GCC.

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

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 11   
@sanderbos4243
@sanderbos4243 Месяц назад
Excellent explanation. The most essential part that I was missing was that the compiler secretly inserts a pointer at the beginning of every instance of a virtual class, pointing to that virtual class its vtable, so that a copy of the vtable doesn't have to be stored in the instance.
@Yuvaraj-pd6ng
@Yuvaraj-pd6ng 5 месяцев назад
it was quick but clear , this is what i was looking for
@MegaGhostek
@MegaGhostek 2 года назад
Nicely done! The editing is brilliant!
@jimmyji8339
@jimmyji8339 8 месяцев назад
Concise and clear
@jojofedouchi1456
@jojofedouchi1456 Год назад
that was a great example
@KKk-xw3ri
@KKk-xw3ri 2 года назад
Helpful video
@MacioSzekTV
@MacioSzekTV 2 года назад
nice video!
@VoidloniXaarii
@VoidloniXaarii 9 месяцев назад
Thank you
@industrialdonut7681
@industrialdonut7681 9 месяцев назад
My question is, why doesn't the compiler just insert the correct method address at object construction instead of going through this whole vtable business? When I look at the assembly that gcc generates for example, when an object of the given class is constructed, it loads whatever data members there are for the instance onto the stack, and then loads the *address of the vtable record for the function*. WHY can't it just, instead of loading the vtable address+offset, load the function address here, itself? What do we gain from using the vtable? I thought at first it was explained by easing compilation, but really it's no more of a burden on compilation to use the method address directly here, since the linker will still just resolve the symbol in any calling code that constructs one of these objects. I must be missing something.
@LukaszLipski
@LukaszLipski 9 месяцев назад
That's a good question, let me try to answer. In some cases it is true that the compiler can figure out the final address of the function. That is true especially when all necessary information is available in the scope. You can try to put both parent and child classes in the same cpp file and turn optimization on (I'm not familiar with GCC but you can try -O3). The function call should be inlined in assembly, at least that's what MSVC does: pastebin.com/cUHQXuxX (the object is created as usual because memory operations can't be optimized away) However, there are cases when we need or it is easier for the compiler to use dynamic dispatch described in the video. One such case is when you allow a user to pick concrete type of the object that's going to be created like this example: pastebin.com/ZhyLgRVx There's no way for a compiler to be sure which address to get at compile time and this is when vtable comes into play, or more formally, dynamic dispatch. Another case is when the function we're trying to invoke resides in different scope/translation unit or the compiler decides it is not worth to inline the function: pastebin.com/YpQh7TaC The function can't be sure what type of the polymorphic object it has (the same function can be used many times with different objects that share the same inheritance chain) so it decides to use vtable which will work for all of them at the cost of additional instructions. Hopefully that makes it a bit more clear. PS. I know there's memory leak in the examples but to make it easier to read I tried to skip "unnecessary" stuff.
@gim913
@gim913 2 года назад
Two missing things I can think of: a) would be nice to cover diamond problem as well (a.k.a. virtual inheritance: en.wikipedia.org/wiki/Virtual_inheritance) b) fun things can happen with vtbl (and there's one particular compiler that was doing this), when you combine inheritance with overloading (i.e. `virtual void handler(your_fave_ptr_type)` + `virtual const Handler& handler()` - perhaps not the best example, but you get the idea)
Далее
How to get Spongebob El Primo FOR FREE!
01:36
Просмотров 13 млн
Building the world's LARGEST iPhone
32:05
Просмотров 230 тыс.
Data ALIGNMENT in C++
4:23
Просмотров 6 тыс.
Virtual Function In C++
15:08
Просмотров 34 тыс.
C++ cache locality and branch predictability
10:43
Просмотров 82 тыс.