Virtual Functions in C++
The Cherno
·
Intermediate
·8y ago
Skills:
Systems Design Basics60%
Key Takeaways
The video discusses virtual functions in C++, a fundamental concept in object-oriented programming, and demonstrates how to use them to override methods in subclasses, with a focus on dynamic dispatch and the use of V tables.
Full Transcript
hey what's up guys my name is HMO and welcome back to my syphilis gloss series today I'm going to be talking about virtual functions in c++ over the last two episodes we've been talking about classes and object-oriented programming and inheritance and all that stuff and virtual functions are really really important to that whole concept virtual functions allow up to override methods in subclasses so for example let's say we've got two classes a and B B is derived from K many would be the subclass of a if we create a method in the a class and marketers virtual we have the option of overriding that method in the B class to get it to do something else as always this is best explained by an example so let's take a look and we're to create two clauses here one's going to be called anti Z which is going to be our base class the only thing this empty class is going to have is a public method called get a name which is going to return a string because this is just an entity we're just going to return the word entity next we're going to create another class player which is going to be a subclass of the entity class we're going to add a little bit more of this class firstly we're going to actually store a name then I'm going to provide a constructor which allows us to specify a name and then we're going to give it a method called get name which in this case is going to return this name the name that is the member okay cool so let's take a look at how we might use this set up let's see whether we create an ASCII here and I'm going to try and print the get name from that FC then I'm going to create a player I'll call this player turn on and I'm going to print the player's name as well and I'm not going to bother deleting these objects because this program tonus anyway there's no just no use in doing that let's get at 5 to write our code and if we look at the results alright cool looks pretty good well as you've got show no printing everything seems fine however everything that we've written here so far will crumble if we decide to use a concept of polymorphism if I start referring to the player as if it was an entity that's where we run into problem so for example if I create a variable here called empty which is actually going to be assigned to T which is of course a pointer to a player type right player however now I'm just referring to it as an entity if I print this so I'll do entity get name if I run micro here you can see we get entity printing however we would have course expect player because even though we're referring to this entity as an entity pointer it is actually a player it's an instance of the player class possibly a better example is if we had a prince named function which took in an entity and then here we just called see out and see get name and I'll replace these with print name a and print name P so now we've got seemingly one function which takes in any kind of entity of course so you can see that we don't get any compile errors when we try to pass in P because P is an entity right player is an entity and all we're doing here is calling the entities get name function which we would expect to be this get name for our entity and then this get name for our player however if we run our code you consider to get entity printing twice why is that that's there's incorrect there instances happening is because if we just declare our functions normally our methods normally inside our classes then when it comes time to call a method is going to call whatever method belongs to the type and of course if we look at this print main function it takes in an entity points off meaning that when we call the get name function inside entity it's going to look at entity and just call get name that's it however we want C++ to somehow realize that hang on a minute the entity that I passed into here is actually a player so please call this get name function that is where virtual functions come in virtual functions that reduce something called dynamic dispatch which compile is typically implemented by our V table a V table is basically table which contains a mapping for all the virtual functions but our base class so that we can actually map them to the correct overwritten function at runtime in the future I'm going to do a whole in depth video on how we tabled work and all that so stick around for that if you're interested but to keep it simple all you basically need to know is that if you want to override function you have to mark the base function in the base bar as virtual let's go back to our code and I'm just simply going to add the word virtual to the front of this get name function that is in our base up in our entity class it might not seem like much but this basically tells the compiler hey generate a V table for this function so that if it's overwritten you can point to the correct function with this change let's hit f5 to run our code and look at that we've got NC and shown on printing correctly now another thing that we can do that was introduced in tables of 11 is actually marked this overridden function with the keyword override right over here this isn't required of course you can see that we just ran our code without that and it worked fine however you should still do this it goes first of all it makes it a little bit more readable since we now know this is actually an overridden function but also it just helps us a little bit with preventing bugs tutors like spelling mistakes and all that like for example five types get a name with a lowercase and you can see we get an error because there's no such function in the base but for us to override or if we try an override function that isn't marked as virtual you can see it also gives us an error so it's just something that helps us out okay so that's basically what a virtual function is virtual functions aren't free though unfortunately there are two runtime costs associated with virtual functions firstly we have the additional memory that is required in order for us to store that be table so that we can dispatch to the correct function that includes a member pointer in the actual base path that points to the V table and secondly every time we call a virtual function we have to go through that table to determine which function to actually map to which is an additional performance penalty and because of these costs some people just prefer not to use virtual functions at all honestly in my experience I've never encountered this to be so costly that that it would make any difference if I stopped using virtual functions so personally I use them all the time without any issue maybe if you are on some embedded platform which has absolutely terrible performance and every CPU slice accounts maybe then avoid virtual functions but otherwise I really can't tell you not to use them because of performance because because it's just such a minimal impact that you probably won't notice it at all hope you guys enjoyed this video on virtual functions if you have an expression comment just leave them below and I'll see you guys next time goodbye [Applause] [Music] [Music]
Original Description
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Patreon ► https://patreon.com/thecherno
Slack ► https://slack.thecherno.com
Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
BEST laptop for programming! ► http://geni.us/pakTES
My FAVOURITE keyboard for programming! ► http://geni.us/zNhB
FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq
MAIN Camera ► http://geni.us/t6xyDRO
MAIN Lens ► http://geni.us/xGoDWT
Second Camera ► http://geni.us/CYUQ
Microphone ► http://geni.us/wqO6g7K
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from The Cherno · The Cherno · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Systems Design Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI