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 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

The mutable keyword in C++ allows class members to be modified within Const methods, and can also be used in lambdas to enable modification of variables passed by value. Understanding the use of mutable is essential for effective C++ programming.

Key Takeaways
  1. Define a class with a mutable member variable
  2. Use the mutable keyword in a Const method to modify the member variable
  3. Apply the mutable keyword in a lambda to enable modification of variables passed by value
💡 The mutable keyword can be used to enable modification of class members within Const methods, and to modify variables passed by value in lambdas.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →