Destructors in C++
The Cherno
·
Intermediate
·8y ago
Key Takeaways
The video discusses Destructors in C++, their purpose, and how they are used to clean up resources when an object is destroyed. It covers the declaration and definition of destructors, their application to both stack and heap allocated objects, and provides examples to illustrate their usage.
Full Transcript
hey what's up guys my name is Aero and welcome back to my C++ series last time we talked about Constructors in C++ and what they are and how to use them definitely check out that video if you haven't already and today we're going to talk about their evil twin the destructor so similarly how a Constructor runs when you create a new instance of an object a Destructor runs when you destroy an object so anytime an object gets destroyed the destructor method will get called The Constructor is usually where you set variables up or do any kind of initialization that you need to do and similarly the destructor is where you unin ize anything that you might have to or clean any memory that you've used the destructure applies to both stack and Heap allocated objects so if you allocate an object using new when you call delete the destructor will get called if you just have a stack based object then when the scope ends the object will get deleted and thus the destructor will get called let's dive in and take a look at some examples so in the Constructor video we created this wonderful entity class which had multiple Constructors here let's go ahead and add a Destructor a Destructor begins with a tier and then the name of our class so basically the only difference between a construct and a Destructor in terms of Declaration and definition is the actual tier that you put at the front of the destructor that's how you know it's a Destructor in this case we just have a simple class with two members X and Y there is absolutely no clean up we need to do for memory here at all since these two floats are allocated in place with our class we're going to talk way more about memory allocation and all of that complicated stuff in the future so don't you worry but for an example let's go ahead and add a message which tells us that the object has been deleted so I might write something like destroyed entity and then for fun in our Constructor here I'm going to write constructed entity or I guess we should call it created since we called this destroyed and not destructed I'm going to get rid of this Constructor just so we don't get too confused I'll initialize X and Y to Z as well just so that we're not crazy I'll get rid of this as well now since this is stack allocated we will only see the destructure getting called when the main function exits which we're not really going to see because our program will close immediately after that so I'm going to just write a function here which will perform all of our entity operations I'll move all this code over here and I will call function now if I run my code just like this you should see created entity then our print function running which prints X and Y and then finally destroyed entity let's take a deeper look as to how this works I'm going to put a break point on line 28 here next to our entity and hit F5 currently I've got absolutely nothing in the console if I hit F10 here and look back you can see that my entity has been created I'm then going to hit F10 to print and you can see that my X and Y position gets printed and then finally the end of the scope is reached here we're about to jump back to line 34 which is our return address since this object was created on the stack it should get automatically destroyed when it goes out of scope which is right over here so if I hit F10 you can see that destroyed entity gets printed to the console because the destructor is called we jump back to our return address and everything is good so that is essentially what a Destructor is it's just a special function a special method which gets called when an object gets destroyed as for some real world examples as to why you would ever write a instructor well again if there is certain initialization code that you've called in the Constructor you will likely want to uninitialize or destroy all of that stuff in the destructor because if you don't you can get memory licks a good example of that is Heap allocated objects if you've allocated any kind of memory on the Heap manually then you need to manually clean it up if it was allocated throughout the usage of the entity or in the entity Constructor you're likely going to want to get rid of it in the destructor because when the destructor gets called that entity is gone you can also call the destructor manually not something that I see a lot of people doing rightly so it's a bit weird if you end up doing something like that the only reason I can really think of doing that is maybe if you've allocated using new however when you delete it you decide to use something like the free function and then you also want to call the destructor manually in that case I don't know not really use that often but you can do so by just calling e do entity like this as if it was any other function and that will actually call it instructor if I run this code which is innocent enough you can see that it destroys the entity twice seemingly we're not actually releasing any resources here so it doesn't crash it just prints this message twice again not something I would recommend or not something I I see often at all very very rarely I've seen something like this so yeah destructors sorted I'll see you guys next time goodbye you [Music] [Music]
Original Description
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Patreon ► https://patreon.com/thecherno
Slack ► https://slack.thecherno.com
Constructors in C++ ► https://youtu.be/FXhALMsHwEY
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
🎓
Tutor Explanation
DeepCamp AI