From the course: Modern C++: Advanced Techniques and Features

Nullability

- In this section, we're going to take a quick look at how to do null pointers properly, in c plus plus. Quite a short Chapter really, we're going to look at the nullptr variable and the nullptr_t data type, and then we'll compare how that works with how you might have done null pointers traditionally, in days before null pointer came to be. So if you have null pointers, you should be using the nullptr keyword instead of using zero or the word null. The word null isn't actually kind of standard in c plus plus. It's not in the specification of the language, but in practice most compilers do define it in a header and most headers there'll be something like this, probably, hash define null to be zero, okay? Or maybe zero L depends on which compiler use. And so traditionally we've all used null capital null or zero to represent our null point. Does that still works? We can still keep on doing this, but it's not ideal. Really, going forward we should be using the nullptr keyword to designate a null pointer. Okay? So it's more expressive, a pop nus. It still works in much the same way as it as you would normally with a pointer. You can still use a verbal here in IF test. Okay? If you have assigned P one to be null ptr, then it is a null pointer, okay? So you can still check for null pointers using the traditional syntax, here. Basically saying is P one not a null pointer. One thing you can't do is, you can't assign null pointer to an integer. Okay? So kind of null pointer internally behaves like the number zero, but it isn't the number zero. You can't just assign it. You can't just say, oh this is basically just like the number zero, let's assign it to an integer. No, that won't work. That fails, okay? You can't assign nullptr to an integer because it's a pointer as opposed to being an integer. Technically speaking internally, the type of nullptr is nullptr_t. Okay? So that's a new type definition, that's introduced in c plus plus these days, right? So if you are moving towards using nullptr, you probably have old code that's using the traditional approach like zero or null in capitals. You can still keep on doing this and introduce nullptr gradually, piecemeal, not like a big banner approach. So have a look at this example here, P one, I've set P one to be a null pointer using the all new, all syn in and all dancing nullptr keyword. Good. If I've got old code, I could still use null if it was working before, it's going to keep on working. If I've got old code that uses zero, then that will keep on working effectively. Logically you would say that all of these are null pointers, okay? So, and you can compare them even though they've used different syntaxes, they're all basically null pointers. So you can mix, and match nullptr, you can compare it against null, and you can compare it against zero. Okay? It is okay to do that in this case. P one is a null pointer, P two is a null pointer, so that works. P one and P three are both null pointers, so that works as well. Okay? So as you go forward in c plus plus, try to get in the habit of using nullptr everywhere. Instead of using zero, it's a more accurate representation of an null pointer type.

Contents