Static for Classes and Structs in C++
The Cherno
·
Intermediate
·8y ago
Skills:
Systems Design Basics60%
Key Takeaways
The video discusses the use of static variables and methods in C++ classes and structs, including their declaration, access, and limitations. It covers the differences between static and non-static members, as well as their interactions with class instances.
Full Transcript
hey what's up guys my name is the Cho and welcome back to my C++ series I know this is weird I'm in like some Czech Hotel just don't ask questions so today we're going to be talking about what static means inside a class or a struct last time we talked about the static keyword in C++ and what its meaning was if it was outside of a class or a struct and there's a link to that video in the description below as well as a card on the screen so go ahead and watch that but today what does static mean if it is inside a class or a struct in pretty much all object Ed languages static inside a class means a certain thing if you use it with a variable it means that there is only going to be one instance of that variable across all instances of the class so if I make a class called entity and I keep making entity instances I'm still only going to have one version of that variable meaning if one of the instances changes one of the variable meaning if one of those instances changes my static variable it's going to reflect that change across all instances because really there's only one variable even though I've made a whole bunch of class instances and so because of that there's no point of referring to the variable through a class instance because there's like there's like a global instance for that class with static methods you get a similar effect where you just you don't have access to the class instance a static method can be called without a class instance and inside a static method you cannot write code which refers to a class instance since you don't have that class instance to refer to Let's jump in and take a look at some examples examples so over here I'm going to write a struct called entity and I'm going to give it two integers X and Y know that I'm using a struct here but you could use a class it doesn't matter the reason I've chosen a struct here is because I want those X and Y variables to be public and by using a struct they're going to be public by default I've made a video about the differences between classes and structs as well if you haven't seen that there's a link everywhere check it out so right now we have a pretty straightforward entity class we can instantiate it like so set the values to what we want them to B such as 2 and three and we're ready to roll if I want to make another instance of that class I can also do that this time I'll initialize it using an initializer so we'll set it to like 5 and 8 for example and also let's give our entity struct some kind of function so that we can print what we have to the console just using standard C out and there we go so if I come down here and I print e and E1 the behavior here is going to be fairly normal and what you would expect so we should get 2 three and then 58 for our second one and you can see that's what we get however things will change if I make the variable static so if I come over here and I make these X and Y static first of all my initializer here will fail because X and Y are no longer class members so we can forget about that and rewrite our code to be this which again is not strictly written correctly however for an example it will work make sure we're referring to E1 here so you can see we are referring to two different instances or it appears to be so at least if we run this code we're going to get unresolved external symbols because we actually have to define those static variables somewhere and we can do so over here by just writing int entity which is the scope of these static variables then the name of the variable and we don't really need to set them equal to anything if we don't want to we can just write this so now that they've been defined the Linker can link to the appropriate variable and we can run our code however if we do so you'll see that we actually get 58 58 printing twice which is a little bit weird you can see here in our code that it clearly appears that we're setting X and Y to 2 and 3 for the first instance but then 5 and 8 for the second one however remember that those X and Y variables when we made them static we made it so that there was only one instance of those two variables across all instances of our entity classes which means that when I'm changing the second entities X and Y they're actually the exact same as the first one they're pointing to the same memory picture two different entity instances however the X and Y that they're pointing to are shared they're pointing to the same X and Y and as such there's no point in US referring to them like this we can actually just refer to them like this as if they were inside the entity scope because really that's all they are it's like we've made two variables that are inside a namespace called entity They Don't Really belong to the class they do of course in the sense that they can be private and public and they have like so they are still kind of part of a class and not just a nam space but for all intents and purposes they basically are just in a namespace and they have nothing to do with any kind of allocation when you create a new class instance or anything like that so if we were to rewrite our code correctly you can see this makes a lot more sense now why we're getting 58 everywhere because we are actually modifying the exact same variable this is of course really useful when you want to have variables across classes you could of course just create a global variable or instead of a global variable it could be like a stack IC Global variable right so it's still internally linked and all that stuff it's not going to be Global across your entire program however doing that would have the same effect of this so why would you do this the answer is basically just it makes sense to put it inside entity if you have something if you have a piece of information a piece of data that you want to be shared across all entity instances or it makes sense to actually store it inside the entity class because it's to do with entities for organizational purpose says you're much better making a static variable inside this class rather than having some static or Global floating around somewhere static methods work in a similar way if I make this print method static then it's going to work fine because you can see it's referring to X and Y which are also static variables I no longer have to call it like this I can also call it like so in fact this would be the correct way to call it and of course you can also notice that it's clearly going to be printing the same thing because we're running the same exact method twice and in this particular example we don't even need class instances at all because everything we're doing is static however things would break here if we decided to make X and Y non-static but keep our print method a static because static methods cannot access non-static variables and there's a very very simple reason to that some people get a little bit confused as to what static thing can access what non-static thing it's really not confusing at all check this out I'm going to bring back my entity instances and rewrite my code so that we actually do have a separate X and Y for each instance of the entity class I'm going to still leave this print method as static I'm going to get rid of this definition of our static variables and get rid of static in front of X and Y if we were to try and compile this code now we're going to get an error and you can see our error reads illegal reference to non-static member entity X because you cannot access this from a static method the reason is that a static method does not have a class instance I might go a little bit more into how classes actually work in a future video but essentially every method that you write inside a class every non-static method always gets an instance of the current class as a parameter that's how classes actually work behind the scenes there's no such thing as a class they just functions with a hidden parameter of sorts a static method does not get that hidden parameter a static method is the same as if you wrote a method outside of the class if I were to write a print method out side here suddenly this probably makes a lot more sense as to why you can't access X and Y because you don't know what they are now picture that you had this exact same method however an entity actually was passed in as a parameter and your code was converted to this suddenly this works what we've just written here this method that we've just written here is essentially what a class method that is non-static actually looks like when it's compiled and so if we take away this instance which is exactly what we're doing when we add this static keyword to a class method we get errors that's why you're getting errors it doesn't know which entities X and Y you want to access because you haven't given it a reference to an entity so hopefully that makes a lot of sense and that clears everything up next time we're going to take a look at integrating this new found static knowledge into our log class that we've been working on if you guys want to see what that looks like you can look at the how to write a C++ class video as we go along with this series we're going to keep adding to that low class and figure out some new things we can do and kind of keep keep improving it as we learn new Concepts static is incredibly useful for static data data that doesn't change between Class instances but that we actually want to use in our classes thank you guys for watching as always you can follow me on Twitter and Instagram links in the description below and if you really like this series you can support it on patreon.com Theo thank you to everyone who's supporting this and making this possible love you guys I'll see you next time goodbye [Music] he [Music]
Original Description
Twitter ► https://twitter.com/thecherno
Instagram ► https://instagram.com/thecherno
Patreon ► https://patreon.com/thecherno
Slack ► https://slack.thecherno.com
Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb
Static in a global scope ► https://youtu.be/f3FVU-iwNuA
Check out my favourite gear for programming:
Laptop ► http://geni.us/pakTES
Keyboard ► http://geni.us/zNhB
Monitors ► 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
More on: Systems Design Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI