If a pointer is set to NULL wouldn't any references to it or through it also be NULL. Here is a compilable example that will bomb when you try to run it:
#include <string>
#include <iostream>
#define NULL 0
class Seedcoat {
public:
//Seedcoat();
std::string Name;
int Weight;
};
class Seed {
public:
//Seed();
std::string Name;
int Weight;
Seedcoat* ItsSeedcoat;
};
class Apple {
public:
//Apple();
std::string Name;
int Weight;
Seed* ItsSeed;
};
int main()
{
///////Apple Objects Begin///////
Apple MyApple;
Seed MySeed;
Seedcoat MySeedCoat;
MyApple.ItsSeed = &MySeed;
MyApple.ItsSeed->ItsSeedcoat = &MySeedCoat;
MyApple.ItsSeed->ItsSeedcoat->Weight = 2;
if ( MyApple.ItsSeed != NULL) {
std::cout << "The weight of the apple seed's seedcoat is " << MyApple.ItsSeed->ItsSeedcoat->Weight <<".\n";
}
MyApple.ItsSeed = NULL;
if ( MyApple.ItsSeed->ItsSeedcoat != NULL) {
std::cout << "The weight of the apple seed's seedcoat is " << MyApple.ItsSeed->ItsSeedcoat->Weight <<".\n";
}
return 0;
}
So my question is: Why does this
MyApple.ItsSeed->ItsSeedcoat != NULL
return true. I would think it would not because ItsSeed is set to NULL - but it still tries to reference the weight value of ItsSeedcoat - and then it bombs I presume because ItsSeed does not exist. I realize there are easy ways to get around this - this example was just to show the behavior I am observing. Is this anything to be concerned about? - or is this normal behavior? What is/are the reason(s) it was done this way? Thanks.