I'm tinkering around with C++ a little bit again, and I have a quick question regarding a class's variables and instances (my Google searches are not providing anything definitive so far on the matter, but I may also be using the wrong keywords): When creating a new instance of a class (i.e, "Test thing;", or "Test* thing = new Test();"), are the public/private variables of that class instance automatically initialized to zero/null values before/during/after the constructor is run, or do I actually have to initialize them explicitly in a constructor?
I don't particularly have a problem with explicitly specifying each and every variable's initialization, even to zero values, persay; I'd just like to know if I actually have to do that, or if I'd merely be wasting time doing something that would already be done for me otherwise.
Of course you explicitly initialize your data in the constructor. The whole idea of the constructor is to have the object be properly setup when it comes into existence. Having data set to zero may or may not have valid meaning for any specific class, so the compiler can't really assume that is the right thing to do. Now, saying that, there are a few places chunks of memory will be set to zero (basically the same places it will be in C), global and static objects. Generally new will not set the allocated memory chunk, though you can redefine your new operator to do so (though, setting the memory to zero in new, then to valid values in the constructor does seem wasteful). I always make no assumptions and have the constructor fully setup the object.
15 Aug, 2009, David Haley wrote in the 3rd comment:
Votes: 0
It's worth mentioning that when you construct an object, by default all of its data members also have their constructors called. So, with this object,
struct S { std::string str; };
when you construct a new S object using new, 'str' will be properly initialized.
Of course you explicitly initialize your data in the constructor. The whole idea of the constructor is to have the object be properly setup when it comes into existence. Having data set to zero may or may not have valid meaning for any specific class, so the compiler can't really assume that is the right thing to do. Now, saying that, there are a few places chunks of memory will be set to zero (basically the same places it will be in C), global and static objects. Generally new will not set the allocated memory chunk, though you can redefine your new operator to do so (though, setting the memory to zero in new, then to valid values in the constructor does seem wasteful). I always make no assumptions and have the constructor fully setup the object.
I see. Thank you for clarifying that for me.
David Haley said:
It's worth mentioning that when you construct an object, by default all of its data members also have their constructors called. So, with this object,
struct S { std::string str; };
when you construct a new S object using new, 'str' will be properly initialized.
Hmm. I wasn't aware of that one. I think that'll probably save me some future unintended-behavior headaches. Thanks.
str will indeed be initialized to something useful (the empty string), since that's what std::string's constructor does – it allocates memory for the string object and sets everything up so it's a nice 0-length empty string. OTOH, char * and int do nothing more than allocate memory for the variable, leaving it set to whatever is in the memory it was allocated from.
So, moral of the story… if you want everything to start with a clean slate, do it yourself.
16 Aug, 2009, David Haley wrote in the 6th comment:
Votes: 0
Well, technically it's more like those types don't necessarily have constructors in the first place. Constructors are called for those things that have constructors. The constructor isn't what allocates the memory for the object, incidentally.
I don't particularly have a problem with explicitly specifying each and every variable's initialization, even to zero values, persay; I'd just like to know if I actually have to do that, or if I'd merely be wasting time doing something that would already be done for me otherwise.