Note that there are some explanatory texts on larger screens.

plurals
  1. POIf a pointer is set to NULL wouldn't any references to it or through it also be NULL
    text
    copied!<p>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: </p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #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 = &amp;MySeed; MyApple.ItsSeed-&gt;ItsSeedcoat = &amp;MySeedCoat; MyApple.ItsSeed-&gt;ItsSeedcoat-&gt;Weight = 2; if ( MyApple.ItsSeed != NULL) { std::cout &lt;&lt; "The weight of the apple seed's seedcoat is " &lt;&lt; MyApple.ItsSeed-&gt;ItsSeedcoat-&gt;Weight &lt;&lt;".\n"; } MyApple.ItsSeed = NULL; if ( MyApple.ItsSeed-&gt;ItsSeedcoat != NULL) { std::cout &lt;&lt; "The weight of the apple seed's seedcoat is " &lt;&lt; MyApple.ItsSeed-&gt;ItsSeedcoat-&gt;Weight &lt;&lt;".\n"; } return 0; } </code></pre> <p>So my question is: Why does this </p> <pre><code>MyApple.ItsSeed-&gt;ItsSeedcoat != NULL </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload