Two ways of calling default constructor
c++, constructor
Solution
Default constructor for POD types fills it with zeros. When you explicitly define your own constructor you are not initialize `x` and `y` and you'll get random values (in VS debug they are filled with exact values, but in release they will be random).
It is according to C++03 Standard 8.5/5:
<...>To value-initialize an object of type T means: — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; — if T is an array type, then each element is value-initialized; — otherwise, the object is zero-initialized.
`B()` is a value-initialization of temporary which will be used in copy-initialization of `b1`.
In `B b2` there is no initializer specified for an object, so according to C++03 Standard 8.5/9:
If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a non-static object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.
To get zeros for `b2` you could write `B b2 = {};`.
Problem
I have the following code: ``` struct B { //B() {} int x; int y; }; void print(const B &b) { std::cout<<"x:"<<b.x<<std::endl; std::cout<<"y:"<<b.y<<std::endl; std::cout<<"--------"<<std::endl; } int main() { B b1 = B(); //init1 B b2; //init2 print(b1); print(b2); return 0; } ``` When I start program (vs2008, debug) I have the following output: ``` x:0 y:0 -------- x:-858993460 y:-858993460 -------- ``` As you can see b1.x and b1.y have 0 value. why? What's difference between init1 and init2? When I uncomment B constructor I have the following output: ``` x:-858993460 y:-858993460 -------- x:-858993460 y:-858993460 -------- ``` Can somebody explain the reason of this behaviour? Tnx in advance.