How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef:
typedef unsigned char boolean;//that's Microsoft RPC runtime typedef
I'd like to change the following line:
boolean variable = 0; //initialize to some value to ensure reproduceable behavior
retrieveValue( &variable ); // do actual job
into something that would automagically default-initialize the variable - I don't need to assign a specific value to it, but instead I only need it to be intialized to the same value each time the program runs - the same stuff as with a constructor initializer list where I can have:
struct Struct {
int Value;
Struct() : Value() {}
};
and the Struct::Value
will be default-initialized to the same value every time an instance is cinstructed, but I never write the actual value in the code.
How can I get the same behavior for local variables?
T obj;
default-initializeobj
even for built-ins? That's not possible. Or do you just need a way to default-initializeobj
no matter what type it is of? Then, as some answers already say,T obj = T();
is the way to go. – Terpsichoreanboolean variable = retrieveValue()
would not be bad either. And that's less typing :) – Jussive