Reputation: 1310
after declaration the pointer to int is not NULL whereas a pointer to class is NULL.
int *pint;
MyClass *Myob;
if (pint){
cout << "pint is not null";
}
if (!Myob){
cout << "Myob is null";
}
Why aren't pointers to Built in types and pointers to classes behaving the same way?
Upvotes: 0
Views: 78
Reputation: 525
Nope, the pointer to both the in-built and as well as class type have indeterminate value and will lead to undefined behavior. In C or C++, if you write
int a;
or
int *b;
MyClass *c;
then a, b, c will have indeterminate value (or garbage value). If you want to initialize them as nullptr
then you can declare them static
(not a good approach) or initialize them explicitly as int a = 10
or int *b = nullptr
.
You should always initialize pointers to NULL
or nullptr
(if your compiler supports C++11, assigning NULL to pointers is deprecated).
Upvotes: 3