ENUMS in C++

The Cherno · Intermediate ·8y ago

Key Takeaways

Explains the use of enums in C++ for organized code

Full Transcript

Hey, what's up, guys? My name is Cherno, and welcome back to my C++ series. Today, we're going to be talking about enums in C++. So, enum is short for enumeration, and basically all it is is a set of values. If you want a more practical definition, then all an enum is is a way to give a name to a value. So, instead of having a bunch of integers called ABC, we can have an enum which has the values ABC, which correspond to integers. It also helps us define a set of values, so that instead of just having an integer as the type, which of course you could assign any integer to, you limit which values can be assigned to what. So, without complicating things, that's really all that it is. It's just a way to name values. It's very useful for when you want to use integers to represent certain states or certain values. However, you want to give them a name so that your code is more readable. At the end of the day, an enum is just an integer. However, in your code it looks a little bit different, and it helps to keep your code a bit cleaner. Let's dive in and take a look. So, suppose that I have three values that I wanted to take care of. I had A, which I might set to zero, B, which I might set to one, and C, which I might set to two. So, I have three values to take care of. And then, somewhere in main, I might use my value, and I might set it equal to one of these three. Later on in my code, I might have some code to check what the current value is, and then perform some sort of action. This is fine, however it carries a few issues. First of all, these ABCs are not grouped at all. Somewhere later in your code, you might have a D, or you might want to declare A again, and suddenly this is a bit of an issue. But essentially, the biggest problem here is that these aren't grouped at all. Furthermore, they are just integers, meaning I can assign something like five here, and suddenly this doesn't make any sense anymore. We want to be able to essentially define a type which can only be one of these three, and is also grouped in some kind of fashion, which is exactly what enums can be used for. So, instead of this, I can have my enum, which I'll call example, and then I can list off which values I I to have here, such as A, B, or C. Instead of having an integer as my type, I can now use the enum name as an actual type. So, I can write example value. I'm going to change my integers to be lowercase here, just so that I can use my enum values properly. And you can see that I can assign my value just like this. If I try and assign something else, I'll get an error because it has to be one of these three. Now, these are just integers at the end of the day. Keep that in mind. I can still do a check if I If I revert this back to B, I can still do a check such as value equals equals one because B does carry the value one. If you hover your mouse over this, it'll even tell you that B is one. You can actually specify the value of each of these variables as well. If you don't, by default, the first one will be zero, and then it will increment one by one. To specify value, you just set it equal to something. So, I could have this as zero, this is two, this is six, whatever you really want. If you start from a number that is not zero, such as five, and you don't actually specify the rest of these values, you can see that this will be six, and this will be seven because it will basically just start from five and then keep going up. One other thing that you can also do here is specify which type of integer you want this enum to be, and you can do so by writing a colon and then the data type. So, for example, an unsigned char. Enums, by default, are 32-bit integers. However, you can see in this case, there's no need for us to use a 32-bit value. We could We could get away with using an 8-bit integer, such as an unsigned char, and we'd be just fine. And suddenly, this is taking way less memory. You won't be able to assign this to something like a float, of course, because the float isn't an integer, and it has to be an integer. So, let's leave this as a char for our example. So, there we go. That's basically what an enum is. It's just a way to give a name to certain values so that you don't have to keep dealing with integers and be all over the place. Let's take a look at a real-world example of where you might use an enum by looking at our log class that we started writing in the how to write a C++ class video. If we jump over here to our log class, you can see what we actually ended up doing was using three different log levels, and they were just integers here, 0 1 2. Hopefully, you can see that this is pretty much a perfect candidate for an enum because we have three values which we just used as integers to represent a certain state. In this case, which log level our log will actually display. So, right over here inside this log class, I'm going to create a level enum. I'm going to create error, warning, and info as possible values. So, we're basically mirroring this. As a matter of style, I actually like to explicitly write equals zero. However, that's definitely not required because it will start at zero by default. But again, just something I like doing to help the readability of the code. I'll get rid of these now. Instead of having an int as my log level, which I could still have, of course, I could still assign this to info, and since an enum is just an int, this would still work. However, in this case, I would be able to set any value to log level, any integer value to log level, and I want to restrict it to to just being these three. So, I will actually set this to be level. Note restriction that I've created of it must be these three purely a syntactical C++ kind of compiler enforced thing. You can get around it really easily. It's not something that can't physically be set, of course. In this case, our enum is just a four-byte integer. It's just four bytes of memory. You can put anything you want into that memory. However, by setting the type to level, you're restricting the code, so to speak, to actually only allow levels. I'll change my set level to take in a level. Now, when I do these checks, they will still work just fine. I just need to change log level error to just be error. Because m_log_level is this enum type, which is an integer, I can still do comparisons between them, which is very, very useful for things like this. I'll change this to say warning, and I'll change this to say info. The other thing to change is here is the log.set_level. Instead of being log.log_level_error, which used to be that integer, it now simply becomes log error because we have an enum value called error inside our log namespace. Note that this enum level is not a name space in itself. There is something called an enum class, which we will talk about in another video. However, just for regular enums, this level isn't really a name space, so you can't use it as one. Which means that this error, warning, and info simply exists inside this log class. As you can see, I can set it to something like warning as well, and everything's fine. Now, you might have noticed the error doesn't actually work, and it gives us an error. The reason this is happening is because we actually have a function called error as well, so same name. And I've set this up deliberately to show you what happens if you have a conflicting name. Now, in this case, unfortunately, that wouldn't work because when we try and refer to things like error here, it doesn't know if we're referring to the function or not. So, we definitely can't have something called error here, unfortunately, if we want to have a function with the same name. So, for now, we can just add a level prefix to the enum, which, by the way, is very, very common practice, so that we know exactly what we're referring to. Since this name doesn't really mean much in the scope of things. So, I'll set this equal to level info, this to level error, this to level warning, and this to level info. And now, of course, I don't need this. I can get rid of this and just set this equal to level error. If I run my code, you can see that I would get the result that I expect, where only error is printing, of course, because we've set our level to be error. So, there you go. Enums just essentially exist to make our lives easier and to make our code cleaner. They are just integers behind the scenes, and you can use them pretty much in any way like integers. There are many, many uses for enums, which we will cover in future videos. We'll definitely talk about enum classes as well in another video. But, essentially, whenever you have a set of values which you want to represent numerically, an enum is probably the way to go. I'll see you guys next time. Goodbye.

Original Description

Twitter ► https://twitter.com/thecherno Instagram ► https://instagram.com/thecherno Patreon ► https://patreon.com/thecherno Slack ► https://slack.thecherno.com In this video we're going to take a look at what enums are in C++. Enum is short for enumeration, which is a set of named values. We can use these instead of normal integer variables to keep our code a little more organised. How to Write a C++ Class ► https://youtu.be/3dHBFBw13E0 Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb 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 Slack ► https://slack.thecherno.com Stream ► http://www.twitch.tv/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno
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
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →