CONST in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is a channel welcome back to my state boss boss series today when we talking all about the Const keyword and C++ a lot of people seem really confused by this so hopefully this video will clear things up so consti is more or less what I like to call a bit of a fake keyword because it doesn't really do much in the scope of changes through the generated code what it is is kind of like visibility for classes and structs it's just a mechanism we get in order to kind of make our code look a little bit cleaner and for certain rules on developers working without code Const is basically sort of like a promise that you give in which you promised that something will be constant that is it's not going to change however it's just a promise and you can bypass that whole promise and you can break your promise just like you can in real life like when I promise to make like daily videos and they're done yeah but anyway the point is that it's just it's a promise you promised something to be constant and whether or not you keep that promise it's kind of up to you but again it's a promise in the sense of you should be keeping that promise and the reason that we want to keep constant Pro kind of promises is because it can actually help simplify our code a lot and ask a lot of other benefits which we'll talk about in a minute so let's just dive into some code so I can show you what I'm talking about if I declare an integer just like this I'll set it equal to five I'm free to change that integer to whatever I like anyway down the road however if I declare this is a constant I cannot change it as you can see here so if I writing Const here you've kind of done a few things first of all you've seen tactically kind of specified that this a integer here is going to be a constant I'm not going to modify it this makes a lot of sense if you declare something like max age and set it equal to 90 or something like that you don't really want this to be a variable because well it's not variable you defined a maximum age and you're never going to change that that's just kind of a number that you need to keep around in your program so this is probably the most simple example for how Const can be used it's simply a way to say that I'm declaring a variable and I'm not going to modify this variable I don't really want it to be a very variable right because the term variable implies that it can change whereas Const stands for constant which means that you're basically declaring a constant instead of a variable something that will not change now there are several other uses for consulates talk about them the first one applies with pointers when you just bear a pointer so for now all I'm going to do is create an integer however I'll Predators integer on the heap so that we actually get a pointer because this is declared without constant or anything I can do two things here I can dereference a and then instead of equal to a value such as two and then of course to get by print a I'll get two and all as well and then the other thing I can do is actually also reassign the actual pointer so that it points to something else like for example this max age that I've got here now to bypass this whole constant I can cast this to a normal eight pointer not something you should usually do remember how I said that you can kind of break the cost promise this is one of the ways however if you try and do it in this case you can see we've declared a max age as an actual constant chances are the compile is actually going to treat that as kind of a read-only constant and if you try and dereference this and actually write to it you'll probably get a crash however for this purpose it will still work if I have here five now you'll see that i now get ninety printing because what we've actually done here is reassigned the pointer so we can do two things we can change the contents of the pointer so the contents at bad memory address but then we can also change which memory address but kind of pointing towards now let's start adding cost everywhere so the first thing I can do is put Const just at the front here so which is a Const int pointer what does that mean that means that you cannot modify the contents of that pointer so you can see here that I've created a pointer however when I when I try and dereference that pointer and change the value of a you can see that I can't do that the value of a being the contents at that actual memory address however reading a of course is still fine you can see I'll be referencing it here printing it and I get no errors you can also notice that I'm not getting any kind of error when I try and actually change a so when I change the pointer aid to point to something else such as Max age that's not a problem I just can't change the contents of that pointer so the data at that we address the second way that I can use Const is by putting it after this point assigning like this what this does is kind of the opposite I can change the contents of the pointer but I can't reassign the actual pointer itself to point to something else note that if you put Const over here right so basically it's before the point it's after the int but it's before the pointer this has the exact same functionality as if I would have written it like I did before like that right Const in pointer or in cons pointer they mean the same thing you just be moved constant that the key here is that it's before the pointer sign right it's before the asterisk whereas to make the actual pointer constant so that we can't reassign the pointer you need to put it after the asterisk before the variable name to make sure you remember that because sometimes you will see people with different kind of programming styles writing into Const pointer but just know that it's the same as constant in pointer the difference is when you do endpoint a Const that's the difference so this is not possible I can't set it equal to anything else like a null pointer or anything I can't erase I actually actual a but I can change the contents of what that pointer is pointing to us and finally of course I can write Const Weiss like this which means that I cannot change the contents of the pointer and I can't change the actual pointer itself to point to something else so that's kind of a second usage of Const when when you're dealing with pointers you can be talking about the pointer itself or the contents of of where the point is pointing towards and where you put cost here with your declaration whether it's to the left or before the asterisk or after the asterisk as you can see it has a different meaning now the last meeting a cost that we're going to talk about today is to do with classes and methods so that let's write a quick class we will call it entity we're going to give it two variables will have my name X what about an image like these or anything is just an example and I'm going to attempt to write getters and setters for this now when I saw already might get X I'll just make it return X I'm actually going to put Const on the right side of the method name so after the after any parameters that we might be taking on going right the work Const so this is kind of a third usage of if it comes kind of not really to do with a variable but after a method name this only works in a class by the way what this means is that this this method is not going to modify any of the actual plus so you can see we cannot modify class member variables if I try and do something like mm x equals 2 I'm not going to be able to do that right I've promised that this method is not going to modify the actual class it's just kind of a read-only method it's just gonna read data from the US potentially but no modifying is gonna be taking place here so it makes sense to write const with a getter however with a setter of course if I wanted to have a setter where I set my x value here I'm going to have to write 2x so I can't declare this as constant because obviously I need to write to the class so this is Const and typically you would declare this with a Const now of course if X was a pointer and you wanted it to be constant or round to what you could do if we just make s a pointer you could do something like Const and the pointer Const get X constant see we've literally got concert in three times on that one line c++ man out rolls so what this means is that we are returning a pointer that cannot be modified the contents of the pointer cannot be modified and this function this method promises not to modify the actual entity class so yeah that's a lot of lot of restrictions we've put onto this method let's revert this back to not being a pointer one thing I'll point out just quickly haha point out but anyway one thing I'll point out really quickly here is that by putting the pointer next to the type like I've done here and this actually become the points I've been my is just still an integer if you want everything to be a pointer on one line like this you actually have to stick a point next to each variable just something I thought I'd mention even though it's a bit off-topic because I'm sure people are gonna be a little bit confused by that potentially so I'm reverting back to just having a normal get up the question is why why would I want to declare this as Const like I get that I get that it kind of promises not to touch things in this function and maybe if someone else was extending that function they would say okay cool this is not meant to write to the class however does this actually enforce something the answer is yes it does if we had our entity over here in our main class let's just write an actual practical example potentially I created my entity and then I have a function which prints my entity and I wanted to access my get up so all there's something like C out will have will pass an entity just like this for now pay or get X and we've got a pretty reasonable function here now I want to be able to pass this by constant reference because I don't want to copy the entity class again we'll talk about copying and stuff in a future video but basically I don't want to copy my entity class because that would potentially be space lobbying in this case it's a base so it probably wouldn't be but in general I don't want to copy I don't want to be copying all my objects because that will be slow especially for something that's read-only so I want to be able to pass it by cross reference now here's the thing if I pass this by constraints it means that this entity is const so just like with pointers if this was a pointer i can modify kind of what it's pointing towards so I can set eat a null pointer and that's fine but I cannot actually modify the contents of a so by writing Const reference like this I have the exact same case I cannot modify the entity I can't reassign it something else because remember this doesn't work like it does with pointers if you reassign this reference you're actually changing this object not some other object there's no kind of separation between a pointer and the contents of the pointer because with the references you are the contents right that's all you can modify those already referencing you are that entity even though you're a reference and so the big thing is I can modify entities so if I remove this Const from this getup suddenly I'm not allowed to call that get X function because this get X function does not guarantee that it's not going to touch the entity it could be doing stuff like this so how on earth would that work I'm not modifying the entity directly however calling a method that does modify the entity that's not allowed so what I have to do is Marcus Const and then what that means is that when I have my const entity i can call any cost functions so because of that you'll actually sometimes see two versions of a function one which just returns X for example with Norah Const and one that returns X for the constant of course in this case it would be using the Const version or the HDX otherwise it will be the other one it looks a bit messy with having two identical functions basically but that's that's how it works so because of that remember to always always mark your methods as Const if they don't actually modify the class or if they're not supposed to modify the class because otherwise you'll literally be stopping people from being able to use it if they have a Const reference or something like that now in some occasions you do have something that is kind of constant you really want to mark the methodist Const but for some reason you just you just needed to modify some kind of variable so suppose that we maybe had a Tanner a variable here which we just needed to modify maybe it was something that's just like for debugging or like it doesn't really affect the program like we still want to mark the methodist cause but we just need to touch this variable well we can do that there's a keyword and see what's bus called immutable immutable of course means that it's able to be changed so if we make this VAR variable mutable you can see that we are modifying it even though we're inside a constant method right we can't modify it if it's not mutable but if we mark it as mutable there we go we can modify it so hopefully that's another question answered what is mutable that's what mutable is it allows functions which are constant methods which are cost to modify it the variable anyway I hope you guys enjoyed this video if you did you can hit that like button to let me know that you enjoyed this video if you really like this video you can help support this series on patreon by going to patreon account for special teacher no don't get some pretty cool rewards such as being able to contribute to the planning of these episodes and talk about stuff and get these episodes early sometimes as well pretty cool stuff and of course you're helping to support this series if you have any questions about Constance or if you feel I haven't covered thing and maybe I should in a future video just leave a comment below I'll try to answer to as many as I can and I will see you guys next time goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 145,554
Rating: 4.9812927 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, const, c++ const, pointers, references, classes
Id: 4fJBrditnJU
Channel Id: undefined
Length: 12min 53sec (773 seconds)
Published: Sun Aug 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.