144: Data Types: C++ nullptr Is Not Zero.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
nullptr represents a null pointer and while it has a value of zero, the type is not the same. For a long time in C++ there was a common rule that said you shouldn’t overload methods with integral types and pointer types. You could and the compiler would let you but it would likely cause problems later. First, let me describe the situation better. Let’s say you have a method called buy in an adventure game that allows your hero to purchase food, clothing, or just about anything. You want to create a couple versions of this method and overload them so they’re both called buy. That means they need different signatures and you decide that one will take a pointer to some other data structure that describes what item the hero wants to buy and how many. For the other method, you want it to just take an int. The int will be used to specify how many additional items to buy. Sort of like a repeat option. The idea is that you normally call buy with the pointer and can sometimes call buy with an int if you want to buy more of the same thing. Maybe your hero lives in a modern age and is careful to avoid leaving any trail of past purchases. So you want to be able to call buy with a pointer value of null to clear out any record of the previous purchase. The problem comes when you try to call buy and pass either a literal value zero or NULL. NULL is a macro that’s a little easier to spot in code because it’s written in all caps. The compiler never sees the value NULL. It just sees the 0. Other than the fact you can’t really count on NULL being an int or a long, it’s the same thing as if you pass a zero directly to the buy method. Now, what does this mean? Which overloaded method should be called? If you pass a zero or NULL as an int, then it will end up calling the buy method that takes an int. I mean, you passed an int, so it makes sense that the compiler would select the method that takes an int. Instead of clearing out your previous purchase, you just bought zero more items as last time. That’s probably not what you wanted. And if NULL is defined to be a long, then your code won’t compile. Again, not the result you’re after. The problem is caused by the fact that the only way to represent a pointer with a null value used to be to use an int value of zero. If all you have to choose from is a method that takes a pointer, then the compiler would treat that zero value as a pointer to null and go ahead and call the method. But when you also have a method that takes an int then that one gets called instead. Listen to the episode for an explanation of using nullptr to solve this problem. Or read the full transcript of the episode below. Transcript For a long time in C++ there was a common rule that said you shouldn’t overload methods with integral types and pointer types. You could and the compiler would let you but it would likely cause problems later. Let me explain and then I’ll also explain how you can use nullptr as a much better solution. First, let me describe the situation better. Let’s say you have a method called buy in an adventure game that allows your hero to purchase food, clothing, or just about anything. You want to create a couple versions of this method and overload them so they’re both called buy. That means they need different signatures and you decide that one will take a pointer to some other data structure that describes what item the hero wants to buy and how many. For the other method, you want it to just take an int. The int will be used to specify how many additional items to buy. Sort of like a repeat option. The idea is that you normally call buy with the pointer and can sometimes call buy with an int if you want to buy more of the same thing. Maybe your hero lives in a modern age and is careful to avoid leaving any trail of past purchases. So you want to be able to call buy with a pointer value of null to clear out any record of the