The Mutable Keyword in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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]
Info
Channel: undefined
Views: 73,118
Rating: 4.9766765 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, mutable, immutable, const, c++ mutable, mutable keyword
Id: bP9z3H3cVMY
Channel Id: undefined
Length: 6min 56sec (416 seconds)
Published: Mon Aug 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.