The Mutable Keyword in C++
The Cherno
·
Intermediate
·8y ago
Key Takeaways
The video discusses the use of the mutable keyword in C++, covering its application in class methods and lambdas, and how it interacts with Const correctness.
Full Transcript
hello guys my name is a Chennai welcome back to my say plus plus series today we're going to be talking all about a keyword called mutable and how we can use it so mutable actually has two fairly different uses one of them is to do with Const if you don't know how constants work in C++ you can check out the video that I made yesterday about that just click on the card or the link in the description below and the other use is actually with lambdas so we'll kind of cover both of them I know I haven't actually talked about lambdas and what they even are a few of you're probably wondering what I'm talking about but definitely will in the future but I thought just make a quick video to cover what mutable actually means because the two cases are actually rather different the word the English word mutable of course means that it's something that is liable to change it's something that can change and you've probably heard me say immutable before meaning that something cannot be changed mutable as the opposite of that means something can change so when we talk about mutable in the context of Const obviously we're talking about something that is kind of Const but actually can change so it's almost like mutable reverses the meaning of Const we really only have one application of mutable in that context and that is with class methods so let's take a look at that I'm going to create a class here called entity I'm going to give it one private member which is going to be in name I'm also going to write a getter for this function so we'll have a Const STD string reference we'll call it get name it will good function will be marked as Const if you don't know what that means and check out the video that I made yesterday about constants a plus plus and this will just basically return our name okay so we have a fairly basic function here together I'm actually going to move this down over here the Const over here means that were not allowed to modify the actual class members so I can't for example reassign name to be something else and really this applies to every variable the primary reason for making these methods Const in the first place and kind of promising that you're not going to touch the class is because if we actually do wind up having some kind of Const entity object over here we would be able to call those cost methods whereas if they weren't marked as Const we actually wouldn't be able to do that so that's why we mark these methods as Const the first place however in some situations we kind of do still want to market Methodist Const because for all intents and purposes it is constant it's not modifying the object they say but maybe just maybe it needs to just touch that variable over there that's kind of in the entity class technically but not really meant to kind of be there I'll give you an example let's just say that for debugging purposes we wanted to count how many times this function was called in our program we might have some kind of integer pulled debug count or something which is initialized to zero and every time we call this function we just wanted to increment that count now we can't do that here so a solution would be to kind of get rid of Const and then we would be fine right but now we can't call it over here and we've kind of broken this and really this is just a getter for name so surely we can kind of just increment this without losing the constants of this method and of course we could move this out into some other class or something and that will be totally fine but that's going to be messy because this applies specifically to this functional through this class so what we can do is restore our quantity R and just mark this variable debug count as mutable I mean we're allowing constant methods to change it and now all was well in the world we can do this Constanta seed stuff here and we can also have a nice constant method which actually does modify this particular class member so marking a class member is mutable means that constants inside that class can actually modify that member that right there is probably the most common usage of mutable by quite a quite a big margin using mutable with class members like this is probably the only time you'll ever use it to be honest but there is however one more use for mutable and I might as well cover that today it's to do with lambdas so without making this too complicated because I haven't covered lambdas the I suppose that we had some kind of bearable here we'll make in X equal Sarah and then I wanted to declare some kind of so I'll just call it ass and I'll write some code over here maybe do something like Prince how low to the console I don't know it could be really anything that's just a lambda a lambda is basically like a little throwaway function that you can write and assign to a variable quickly like we've done here we can call a lambda just like any other function by using its name like this and just specifying any parameters we might have now suppose that we actually wanted to use x over here like instead of printing color I want it to print X that's going to be fine I can just pass in X like this however I do need to define some kind of capture method so we can either send this variable by reference like this or by value like that or you can just type in equals or absent to send everything basically that's using here by reference or by value now suppose that this lambda actually did some extra stuff maybe did X plus plus however we still wanted this to be by value you'll see that if I actually change this to be by a value and I try to do something like X plus plus I get an error and what I actually would have to do in this situation is maybe make another variable assign it to this and then kind of essentially incremental or modify that variable I have to copy it and I have to basically create a local variable which can actually take the value from the variable and passing in this is a little bit messy so what I could do instead is use that wonderful mutable keyword so I'll go back to doing this and all I have to do is say that this lambda is mutable by just sticking that mutable keyword right there what this means is that variables that you passed by a value like I just did with this X you can change them and of course what this will do is basically what I showed you with that y example it's going to create a local variable item it's just that in your source code it's going to look a lot cleaner and of course outside of this function so like after I call this lambda over here X will still be set to eight it will not increment and be set to nine you are not passing it by reference now like you would if you did this you're still passing it by value so it still will be eight here because you're just copying that value eight into this lambda alright that is what the mutable keyword is again as I said earlier you'll be using it with classes and Const like 90% of the time I honestly do not think I've ever written it inside a land like the before it just it just doesn't happen in practice I haven't even seen code like this very often at all if you have any more questions about mutable or constants or anything you can leave a question in the comment section below I'll try to respond to as many as I can if you guys enjoy this video you can show me the e did by hitting that like button below and you can also support the series on patreon to make sure that more episodes are made by going to the patreon account for sex Cherno and get some cool rewards like seeing excess early and contributing to the planning and suggesting topics and all that fun stuff I'll see you guys in next video goodbye [Music] [Music]
Original Description
Patreon ► https://patreon.com/thecherno
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Slack ► https://slack.thecherno.com
Constants ► https://youtu.be/4fJBrditnJU
Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
Gear I use:
-----------------
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