Here you may be confused by undefined behavior. There are no rules in the C ++ standard as to what happens if you use an object after running its destructor, as this behavior is undefined, and therefore the implementation can do whatever it likes. As a rule, compiler developers do nothing special for undefined behavior, and therefore there is an artifact of what was done by other design decisions. (Sometimes this can cause very strange results.)
Therefore, after starting the destructor, the compiler has no additional obligations regarding this object. If you do not reference it again, it does not matter. If you refer to it, this behavior is undefined, but from a standard point of view, the behavior does not matter, and since the Standard does not say anything, most compiler developers will not worry about what the program does.
In this case, the easiest way is to leave the object untouched, since it does not hold resources, and its storage was allocated as part of the function launch and will not be returned until the function exits. Therefore, the value of the data item will remain unchanged. Naturally, what the compiler should do when it reads ob2.day is access to the memory cell.
Like any other example of undefined behavior, the results may change with any change in circumstances, but in this case, they probably will not. It would be nice if the compilers would catch more cases of undefined behavior and diagnose problems, but compilers cannot detect all undefined behavior (some of them occur at runtime), and often they don’t check the behavior I don’t think is possible.
David thornley
source share