F
Frederick Gotham
I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.
Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).
Here are some examples of the former:
struct MyStruct {
int a;
double b;
void *p;
};
int main()
{
int a; /* Contains garbage */
double b; /* Contains garbage */
void *p; /* Contains garbage */
MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:
struct MyStruct {
int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */
double b = double(); /* Contains 0.0 */
typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */
MyStruct obj = {}; /* Each member has the same values as above */
}
The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.
What is zero-initialisation, and can anyone give an example of it please?
What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?
template<class T>
struct Init {
T obj;
Init() : obj() {}
};
of the different kinds of initialisations.
Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).
Here are some examples of the former:
struct MyStruct {
int a;
double b;
void *p;
};
int main()
{
int a; /* Contains garbage */
double b; /* Contains garbage */
void *p; /* Contains garbage */
MyStruct obj; /* All members contain garbage */
}
And here are some examples of the latter:
struct MyStruct {
int a;
double b;
void *p;
};
int main()
{
int a = int(); /* Contains 0 */
double b = double(); /* Contains 0.0 */
typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */
MyStruct obj = {}; /* Each member has the same values as above */
}
The Standard tells me that there are three kinds of initialisation:
Zero-initialisation
Default-initialisation
Value-initialisation
In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.
What is zero-initialisation, and can anyone give an example of it please?
What is value-initialisation, and can anyone give an example of it please?
Also, what kind of initialisation does the following class perform?
template<class T>
struct Init {
T obj;
Init() : obj() {}
};