struct A { int x; A() : x() {} };
struct B { int x; };
int main()
{
A a;
B b;
return a.x == b.x;
}
The value of a.x is always 0. The value of b.x is indeterminate or
uninitialized.
This is called default-initialization, right?
consider:
B b( B() );
... b.x is now initialized.
I don't quite understand this... "B()" creates a temporary object and
it's passed to a copy-constructor to initialize "b", and the copy
constructor tries to copy "x" value of the tmp object to "b"'s "x".
However, what is the value of tmp object's "x"?
You need to read the chapter on constructors in C++ in your favorite
book and come back.
I did read Accelerated C++, but am still confused. In order to
clarify the matter, here's what I think, not sure whether they are
right/wrong:
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)", or when we invoke copy constructor. 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?
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?
I'd really appreciate it if some of you could answer my questions and
point out my mistakes and also let me know the points I overlooked.
Thanks.
Jess