This is going to sound so basic as to make one think I made zero effort to find the answer myself, but I swear I did search for about 20 minutes and found no answer.
If a private c++ class member variable (non-static) is a pointer, and it is NOT initialized in the constructor (either through an initialization list or an assignment in the constructor), what will its value be when the class is fully instantiated?
Bonus question: If the answer to the above question is anything other than NULL, and I wish to always initialize a particular member pointer variable to NULL, and I have multiple constructors, do I really have to put an explicit initialization for that pointer in every constructor I write? And if so, how do the pros handle this? Surely nobody actually puts redundant initializers for the same member in all their constructors, do they?
EDIT: I wish I could’ve chosen two answers here. The smart pointers recommended by Bleep Bloop seem to be the elegantest approach, and it got the most up votes. But since I didn’t actually use smart pointers in my work (yet), I chose the most illustrative answer that didn’t use smart pointers as the answer.
You’re thinking correctly. If you don’t initialise it, it could be anything.
So the answer to your question is yet, either initialise it with something, or give it a NULL (or nullptr, in the most recent C++ standard).
Our default ctor here makes it NULL (replace with
nullptrif needed), the second constructor will initialise it with the value passed (which isn’t guaranteed to be good either!).