Re: Are int a; int a(); int a=0; the same?
On Nov 18, 5:03 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
<PengYu...@gmail.com> wrote in message
news:fa72d4e5-48c2-4307-91d3-a6aa63e5d275@a28g2000hsc.googlegroups.com...
I'm wondering if the following three statements are the same or only
the last two are the same?
int a;
int a();
int a=0;
You missed one
int a(0);
The three you show do different things.
The first one declares a as an interger which is unitialized.
Can contain any value. Be careful, some compilers will
initialize variables to 0 in debug mode but not release mode
sometimes causing hard to find errors.
In debug mode, I'd expect something like 0xdeadbeef.
And of course, it depends where the declarations are placed. If
they are at namespace scope, then the first one is zero
initialized.
The second one declares a function called a accepting no
parameters and retuning an integer.
The third one declares a as in integer and initializes it to
zero.
The fourth one also declares a as an integer and initializes
it to zero.
Note:
int a;
a = 0;
is different than
int a = 0;
The first case uses the assignment operator. The second case
uses the constructor.
If you throw constructors into it, then "int a(a);" and "int
a=0;" are different as well:-). Still, the first is assignment,
and the second initialization, and there are cases, even with
int, where this makes a difference:
switch ( something )
{
case 0 :
int a = 0 ; // Illegal...
break ;
case 1:
int b ; // Legal...
b = 0 ;
break ;
}
(How's that for adding to the confusion:-)?)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34