So do these two functions accomplish the same thing in C++?
void MYCLASS::clear() {
classSize = 0;
class_ptr = NULL;
}
void MYCLASS::clear() {
classSize = 0;
*class_ptr = NULL;
}
Where int *class_ptr is declared in my header file. When I hover my mouse over Visual Studio shows class_ptr as the same type.
I believe that they are both pointing class_ptr at NULL. However, I want to make sure that they are indeed both doing this.
Nope…
Sets the pointer’s value to
NULL, i.e.,class_ptr == NULL.Sets what
class_ptrrefers (points) to toNULL, which is0.class_ptritself retains its value.A pointer’s value is an address. What resides at that address is (should be) a representation of the type of the pointer itself, i.e.,
Note that, if there existed no implicit conversion from
int(NULLhas a value of0) to whatever typeclass_ptrrefers to, the code wouldn’t even compile (which leads me to believe that this particular pointer is poorly named).EDIT: I liked @Mooing Duck’s analogy so much that I stole it (he has not posted an answer, so I don’t think he’ll mind).