241: How To Use Raw Pointers Properly And Still Avoid Crashes.
Take Up Code - Podcast autorstwa Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorie:
Some people say that raw pointers are evil and should be avoided. Raw pointers are useful when used properly. This episode explains how to use raw pointers along with smart pointers and is taken from a recent game development session. I have a text-based role-playing adventure game that I’m working on with a main hero in the game. I want the hero to be able to move around. It wouldn’t be very adventurous if the hero had to stay in the same spot. This means I need to keep track of the hero’s location. Because this is a simple game, the location just needs an x and y coordinate. I decided to add a class that would manage multiple groups of properties. Grouping them together seemed like a good idea so it’ll be easier to understand an x property and a y property if they appear in a group called location. Listen to the full episode to understand where and why I chose to use unique_ptr to manage ownership of the groups and property values as well as how I used raw pointers when retrieving groups and property values. Or you can read the full transcript below. Transcript Raw pointers are useful when used properly. Avoiding them entirely would be like a home improvement store deciding to stop selling hammers because too many people were hitting their thumbs. If you use raw pointers the wrong way, then you will get hurt. It takes a little bit extra thought and knowledge that I’ll explain here in order to use them safely. Some languages have decided to remove pointers and I always felt that programmers were the ones to suffer. Specifically, it makes certain designs more clumsy when you try to avoid pointers or when you have no pointers available at all in your language. I’ll also be using examples in this episode from a recent game development session. These are the kinds of real-world applications that I explore in the game development sessions. You can register to attend an upcoming session by going to takeupcode.com and clicking on the Classes link at the top of the page. Each session is reasonably priced and you’ll find them available almost every Saturday. Here’s the scenario that led me to describe how to use raw pointers. Software changes over time and I’ve been known to completely change designs in just a few hours if needed. So the design that I’m about to describe will almost certainly change. I have a text-based role-playing adventure game that I’m working on with a main hero in the game. I want the hero to be able to move around. It wouldn’t be very adventurous if the hero had to stay in the same spot. This means I need to keep track of the hero’s location. Because this is a simple game, the location just needs an x and y coordinate. I have a Character class to represent the hero. Now I could just put a couple integer properties in the class for the x and y coordinates. But I know that I want the game to be extensible. So I’ll eventually need a flexible way to add properties that I don’t know about yet. One good way to do this is to start now. In other words, don’t add fixed properties now and then later try to add dynamic properties later that can change. Anytime you find yourself with special cases where you treat one thing differently than another, you’re just adding extra complexity to your design. In other words, even a simple solution paired up with a more complex solution will overall be more complicated than if everything just used the more elaborate solution. Consistency goes a long way to making something easy to understand and use. So I decided to add a class that would manage multiple groups of properties. Grouping them together seemed like a good idea so it’ll be easier to understand an x property and a y property if they appear in a group called location. Maybe there can be another group called health that will be used when I add the ability to fight monsters in the game. You should also notice that the d