default constructor

With reference to the following example:

class example {
public:
int a,b;
};

int main()
{
example ex1;
cout << ex1.a << endl;

return 0;
}


The question is the following:
As I created a simple class without an explicit constructor, the compiler create a default constructor for me. The role of a constructor, included a default constructor, is to initialize the data members of a class, so I expect that the default constructor automatically initialize the members a and b. Instead, although I haven’t got any compile-time error, I have the following run-time error: “The variable ‘ex1’ is being used without being initialized”. What's wrong with my reasoning?
Thank you.
The default constructor for int does nothing (ie, it does not initialize it to zero).
OK, that's why.
Thank you.
the default constructor would be equivelent to:
example() {}
in order to initialize the variables, you would have to do so yourself.
Actually, the default constructor is this:

example() : member1(), member2(), member3(), ... {}
I don't see any difference.
Oh, so the default constructor is automatically called for variables not in the initializer list? Didn't know that, cool.
I don't see how that could be true firedraco. If what you are saying is true, then the integers would be default initialized and would be set to zero. There was a thread on this a while back where we were discussing the C++ std.
example() : member1(), member2(), member3(), ... {}

According to the C++ std, if member1, member2, and member3 were integers they would be default initialized if specified in an initializer list like that.
The default constructor for int does nothing (ie, it does not initialize it to zero).
The default constructor is created because otherwise you wont be able to create class objects (if you don't provide any constructors).
So example = new example(); will create object but it wont initialize variables.
@firedraco & kempofighter:

The following code in fact does initialize x to 0:

1
2
3
4
class example {
  int x;
  public:
  example() : x() {} // This _does_ initialize x to 0 


There were other threads where I explained why this special provision was added
to the language.

Therefore, the default compiler-provided constructor for example is:

 
example::example() {}

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

struct A{
	int a;
	A():a(){}
};

int main(){
	A a;
	std::cout <<a.a<<std::endl; //without line 5, 2 (garbage); with it, 0
	return 0;
}

What the hell, then? Objects get default-constructed but primitives don't?
The default constructor for int does nothing.

In the above, on line 5 you are not (even though it looks that way) default constructing a.

It's a subtle difference.

It had to be added to the language to allow things like:

1
2
3
4
5
6
template< typename T >
class Foo {
   T x;
   public:
      Foo() : x() {}
};


to work consistently for all types T. Obviously putting an explicit "0" there would
mean x has to be constructible from an int. Therefore things like string would not
work.

Without the subtlety, then upon construction of a Foo<int>, x is uninitialized but
upon construction of a Foo<string>, x would be initialized to a known value.

The language had to solve this problem, so the solution was to allow x() in the
initializer list to initialize x to zero (false) for all POD types, including pointers.
Okay, that makes sense, now. I think I was mixing up the terms.
Actually, the statement
example ex1;

At first, the system will allocate enough memory to store the object ex1
then, the default construct was called to init the object ex1 to init the class member a and b.

As a and b is int,
And the default value to store the new allocate memory is oxcc,
so the a and b is 0xcccccccc in 32 bit machine.

In memory ,
ex1 stored in address of 0x0012ff5c(in my machine),
and the memory in 0x0012ff5c is
0x0012FF5C cc cc cc cc cc cc cc cc cc cc cc cc b8 ff 12 00 48 26 41 00 01 ................H&A..
0x0012FF71 00 00 00 a8 61 38 00 60 38 38 00 60 87

the ext1's data is the cc cc cc cc cc cc cc cc cc cc cc cc .

At the same time, 0xcc is a interrupt,
it's int 3 if you knew assemble language.

so the debugger will tell you the var wasn't init


the default value to store the new allocate memory is oxcc
No, that's just what VC++ initializes stack memory to when the program is built for debug. Heap memory is initialized to 0xCD.
Topic archived. No new replies allowed.