J
Jess
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially Accelerated C++). However, I
don't think my understanding about these concepts is adequate and I'm
not sure under what situations each of these two init methods is
used. Therefore, I'm posting my questions below along with what I
understand about these two init methods. I'd appreciate it if someone
could answer my questions and point out my mistakes. Thanks.
1. Default initialization is used when we create an object using the
form "T t", where T can be a POD type or class type. If this happens,
the following occurs:
a. if T is POD, then "t" is garbage value
b. if T is class, then it's default constructor is invoked. If
there's no default contructor, the compiler will synthesize one.
(i). If there's a default constructor, then all T's POD member data is
initialized according to the constructor initializer list; any POD
member not mentioned in the initializer list gets default-init, with
garbage value. If T has a class-typed member (say of type A), then
A's default constructor is called and if A has no default constructor,
then the synthesized constructor will be called. FInally, the
constructor's body is entered.
(ii). If there is no default constructor, then the synthesized
constructor will be called. All POD member gets garbage data and
class members have their default constructor (or synthesized default
constructors) invoked.
Is this what happens to default-init? Moreover, to init POD member in
the constructor initializer, I can do things like "x(10)" and also
"x()" where "x" is an int, is this right? If "x()" is used, then "x"
gets value-init to 0?
2. Value-initialization happens if a constructor is explicitly invoked
like "T t(arg)", its copy constructor is invoked. For a class type
object:
a. if it's created by invoking a constructor like "T t(arg)", then all
its POD member gets initialized according to the constructor
initializer; any POD member not mentioned gets value-init to 0. But
what happens to those class-typed members? Are their default
constructors (or synthesized one) called? I think after all these are
done, the constructor's body is entered.
b. if the T object is created through copy constructor, then the
values of the existing objects are copied into the new object.
However, what if we have an example like "B b = B()"? what's the value
of the tmp object created by "B()"? Is it the result of default-init
or value-init?
3. To value-init a POD object, I think the only way is to do something
like:
int n = int();
is this right?
4. The book Accelerated C++ also says that if an object is used to
init a container element, either as a side effect of adding new
element to a "map", or as the elements of a container defined to have
a given size, then the members will be value-initialized. I checked
it through an example,
struct A{
int x;
};
int main(){
A a[10];
return 0;
};
Then I found out each element in a has it's "x" value as 0. I think
this is what the author of the book meant. Then I tried another
example:
int main(){
int b[10];
return 0;
}
This time, each element in b has garbage value. Isn't each member of
b supposed to be value-init to 0?
Thanks a lot
Jess
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially Accelerated C++). However, I
don't think my understanding about these concepts is adequate and I'm
not sure under what situations each of these two init methods is
used. Therefore, I'm posting my questions below along with what I
understand about these two init methods. I'd appreciate it if someone
could answer my questions and point out my mistakes. Thanks.
1. Default initialization is used when we create an object using the
form "T t", where T can be a POD type or class type. If this happens,
the following occurs:
a. if T is POD, then "t" is garbage value
b. if T is class, then it's default constructor is invoked. If
there's no default contructor, the compiler will synthesize one.
(i). If there's a default constructor, then all T's POD member data is
initialized according to the constructor initializer list; any POD
member not mentioned in the initializer list gets default-init, with
garbage value. If T has a class-typed member (say of type A), then
A's default constructor is called and if A has no default constructor,
then the synthesized constructor will be called. FInally, the
constructor's body is entered.
(ii). If there is no default constructor, then the synthesized
constructor will be called. All POD member gets garbage data and
class members have their default constructor (or synthesized default
constructors) invoked.
Is this what happens to default-init? Moreover, to init POD member in
the constructor initializer, I can do things like "x(10)" and also
"x()" where "x" is an int, is this right? If "x()" is used, then "x"
gets value-init to 0?
2. Value-initialization happens if a constructor is explicitly invoked
like "T t(arg)", its copy constructor is invoked. For a class type
object:
a. if it's created by invoking a constructor like "T t(arg)", then all
its POD member gets initialized according to the constructor
initializer; any POD member not mentioned gets value-init to 0. But
what happens to those class-typed members? Are their default
constructors (or synthesized one) called? I think after all these are
done, the constructor's body is entered.
b. if the T object is created through copy constructor, then the
values of the existing objects are copied into the new object.
However, what if we have an example like "B b = B()"? what's the value
of the tmp object created by "B()"? Is it the result of default-init
or value-init?
3. To value-init a POD object, I think the only way is to do something
like:
int n = int();
is this right?
4. The book Accelerated C++ also says that if an object is used to
init a container element, either as a side effect of adding new
element to a "map", or as the elements of a container defined to have
a given size, then the members will be value-initialized. I checked
it through an example,
struct A{
int x;
};
int main(){
A a[10];
return 0;
};
Then I found out each element in a has it's "x" value as 0. I think
this is what the author of the book meant. Then I tried another
example:
int main(){
int b[10];
return 0;
}
This time, each element in b has garbage value. Isn't each member of
b supposed to be value-init to 0?
Thanks a lot
Jess