Suppose I have a class with private memebers ptr
, name
, pname
, rname
, crname
and age
. What happens if I don't initialize them myself? Here is an example:
class Example {
private:
int *ptr;
string name;
string *pname;
string &rname;
const string &crname;
int age;
public:
Example() {}
};
And then I do:
int main() {
Example ex;
}
How are the members initialized in ex? What happens with pointers? Do string
and int
get 0-intialized with default constructors string()
and int()
? What about the reference member? Also what about const references?
What else should I know about?
Does anyone know a tutorial that covers these cases? Maybe in some books? I have access in university's library to a lot of C++ books.
I'd like to learn it so I can write better (bug free) programs. Any feedback would help!
If you example class is instantiated on the stack, the contents of uninitialized scalar members is random and undefined.
For a global instance, uninitialized scalar members will be zeroed.
For members which are themselves instances of classes, their default constructors will be called, so your string object will get initialized.
int *ptr;
//uninitialized pointer (or zeroed if global)string name;
//constructor called, initialized with empty stringstring *pname;
//uninitialized pointer (or zeroed if global)string &rname;
//compilation error if you fail to initialize thisconst string &crname;
//compilation error if you fail to initialize thisint age;
//scalar value, uninitialized and random (or zeroed if global)Members with a constructor will have their default constructor called for initialisation.
You cannot depend on the contents of the other types.
If it is on the stack, the contents of uninitialized members that don't have their own constructor will be random and undefined. Even if it is global, it would be a bad idea to rely on them being zeroed out. Whether it is on the stack or not, if a member has its own constructor, that will get called to initialize it.
So, if you have string* pname, the pointer will contain random junk. but for string name, the default constructor for string will be called, giving you an empty string. For your reference type variables, I'm not sure, but it'll probably be a reference to some random chunk of memory.
Uninitialized non-static members will contain random data. Actually, they will just have the value of the memory location they are assigned to.
Of course for object parameters (like
string
) the object's constructor could do a default initialization.In your example:
In lieu of explicit initialization, initialization of members in classes works identically to initialization of local variables in functions.
For objects, their default constructor is called. For example, for
std::string
, the default constructor sets it to an empty string. If the object's class does not have a default constructor, it will be a compile error if you do not explicitly initialize it.For primitive types (pointers, ints, etc), they are not initialized -- they contain whatever arbitrary junk happened to be at that memory location previously.
For references (e.g.
std::string&
), it is illegal not to initialize them, and your compiler will complain and refuse to compile such code. References must always be initialized.So, in your specific case, if they are not explicitly initialized:
You can also initialize data members at the point you declare them:
I use this form pretty much exclusively, although I have read some people consider it 'bad form', perhaps because it was only recently introduced - I think in C++11. To me it is more logical.
Another useful facet to the new rules is how to initialize data-members that are themselves classes. For instance suppose that
CDynamicString
is a class that encapsulates string handling. It has a constructor that allows you specify its initial valueCDynamicString(wchat_t* pstrInitialString)
. You might very well use this class as a data member inside another class - say a class that encapsulates a windows registry value which in this case stores a postal address. To 'hard code' the registry key name to which this writes you use braces:Note the second string class which holds the actual postal address does not have an initializer so its default constructor will be called on creation - perhaps automatically setting it to a blank string.