0

I was trying some different ways of initializing variables.

int a(0);
cout<<a; 

for this code segment output is 0 .

in another way, I initialize a with 0

int a= int();
cout<<a;

output: 0

then I try this:

int a(int());
  cout<<a;

this time output is 1

actually what does the value the int() function return ? 0 or 1

lukai
  • 536
  • 1
  • 5
  • 20

2 Answers2

2

I think that your last attempt (int a(int())) is an example of the "most vexing parse". Thus, a is a function, not an int.

This:

#include <typeinfo>

std::cout << typeid(a).name() << std::endl;

Yields:

FiPFivEE

And putting this result here gives:

int ()(int (*)())
piwi
  • 5,136
  • 2
  • 24
  • 48
  • while i am writing int a(0) . Is a is an int or a function here ??? – lukai Sep 13 '13 at 07:47
  • @lukai You should read this article (http://herbsutter.com/2013/05/09/gotw-1-solution/) that explains the possible syntaxes for initializing a variable – piwi Sep 13 '13 at 07:51
  • @piwi I don't know. First, it only applies to C++11, which most professionals can't use yet. And second, it contains a fair amount of bad advice (like preferring the obfuscating and error prone `auto` forms over the more explicit forms). – James Kanze Sep 13 '13 at 09:30
  • @JamesKanze it does not applies to C++11 *only* – piwi Sep 13 '13 at 09:39
  • @piwi The article you site is full of syntax which is only valid in C++11. Standardizing on the new initialization sequence _is_ a good idea, but it will take a few years before people in industry can do so. Using `auto` everywhere (another recommendation in the article) is a horrible idea, however; it obfuscates, and is very error prone. – James Kanze Sep 13 '13 at 10:00
  • @JamesKanze Regarding `auto`, what do you mean by error-prone? Could you please elaborate? – piwi Sep 13 '13 at 11:51
  • @piwi With `auto`, you don't necessarily know the actual type, and it can change as a result of changes elsewhere. So you start out safe, with a signed integral type, and a change in basically unrelated code suddenly turns it into a problematic unsigned type. – James Kanze Sep 13 '13 at 12:45
0

Doing int() creates a temporary integer and value initialize it.Value initialized integers without a specific value will have the value zero. So the declaration

int a = int();

value initializes a temporary integer, and copies it to a.

However for the third example, as many have pointed out, you actually declare a function named a.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • that means int a(int ()) , in this case int() didn't play any role here. – lukai Sep 13 '13 at 07:55
  • 1
    "Doing int() creates a temporary integer and default constructs it. Default constructed integers will have the value zero.": The correct sentences are, actually, "Doing int() creates a temporary integer and value initialize it. Value initialized integers will have the value zero.". – Cassio Neri Sep 13 '13 at 08:00
  • He declares a function returning `int` and taking a pointer to a function as an argument. He doesn't "create" it, and if he tries to use the function, he would still need a definition elsewhere. – James Kanze Sep 13 '13 at 09:25