Scope Resolution Operator, Namespaces, Enum Classes
Today we made a simple inventory system in C++ using classes and a vector.
There's a couple major learning points for today.
1) The Scope Resolution Operator (::) allows you to select between different scopes (a scope is a pair of matching curly braces) to say which variable or whatever you are talking about. After all, you can have a local variable named x, a global variable named x (called ::x), an x in the std namespace (std::x), an x in the Kerney namespace (Kerney::x) an x in an enum class (ITEM_TYPE::x) an x in a class (classname::x) and so forth.
The :: basically means "inside of" and is used to specify which x you're talking about.
2) Static variables inside of a class are associated with the class, not just with objects of that class. So if you had an Insect class that had a const static int MAX_LEGS = 6, you could cout Insect::MAX_LEGS to print the number 6 before you even make any variables of Insect type.
3) You can use enums to make a list of possible values for a variable. If you have a small finite list (for example, menu options in a GUI) they work really well, and prevent accidental mistakes from using an int or something to represent them.
You make an enum class like this:
enum class OPTIONS {A,B,C};
Then make a variable of this type like this:
OPTIONS bob = OPTIONS::C;
4) Namespaces are a way of hiding your names from the global namespace so that they can be used with other people's code without causing a name conflict.
Other Videos By Bill Kerney
2021-10-25 | The Definition(s) of Racism and Ethnicity |
2021-10-22 | Reading from Multiple Files at the Same Time |
2021-10-22 | The Therac-25 Tragedy and the Impact of Computer Science on the World |
2021-10-21 | UE4 Level Streaming, Math for Game Engines |
2021-10-21 | UE4 Inventory System, Modding Quake 1 |
2021-10-21 | Recursive Maze Solving |
2021-10-20 | Reading from a File into a Vector of a Class |
2021-10-20 | Framing Part II |
2021-10-19 | Intersection Testing |
2021-10-19 | Mergesort |
2021-10-18 | Scope Resolution Operator, Namespaces, Enum Classes |
2021-10-18 | Framing |
2021-10-16 | Fallacies: Appeal to Ignorance, Correlation is Causation |
2021-10-15 | The Society of Creative Anachronism |
2021-10-15 | C++ Member Functions |
2021-10-14 | Recursion Part I |
2021-10-14 | Linear Algebra for Games |
2021-10-14 | Level Design Flow |
2021-10-13 | Constructors and Destructors Part II |
2021-10-13 | Constructors and Destructors, Physics Simulation (Part 1) |
2021-10-13 | Red Herring, Non-Sequitur, Strawman, Ad Hominem, Tu Quoque |