2

What is this:

int();

? Is it a call to a default int constructor? According to this built-in types don't have constructors. Then what is it? I know (more or less) what it does - it returns an int equal to 0:

int a = int();   // after this a == 0

And it looks like a default constructor. So why can't this:

int a;   // local variable

also initialize a with 0? I'm trying to understand how built-in types work and when they get initialized with 0, and when they don't.

Community
  • 1
  • 1
NPS
  • 6,003
  • 11
  • 53
  • 90
  • Because in C++ the philosophy is you don't pay for what you don't need. So, you'd use `int a;` if you don't need `a` to be initialized at that point, and `int a = int();` if you want it initialized. – SleuthEye Oct 20 '14 at 23:19
  • According to your citation the built in types *do* have constructors, unless you're prepared to believe anecdote over the language standard and the author of the language. – user207421 Oct 20 '14 at 23:22
  • keep in mind that "int a;" on its own will only reserve the memory slot, and the value will be whatever was previously in that reserved memory spot. Providing a value intializer will ensure it starts with a default value for that type. I prefer C++11 standard initilizer { }. – Jeff Oct 20 '14 at 23:27
  • @chris, had to connect a mouse to my tablet for the delete icon to show - requires hover – Crowcoder Oct 20 '14 at 23:34

0 Answers0