Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13682 > unrolled thread

why bug in the program

Started byprava sahoo <spravasini@gmail.com>
First post2012-01-31 22:45 -0800
Last post2012-02-01 00:14 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  why bug in the program prava sahoo <spravasini@gmail.com> - 2012-01-31 22:45 -0800
    Re: why bug in the program Miles Bader <miles@gnu.org> - 2012-02-01 16:02 +0900
    Re: why bug in the program Gareth Owen <gwowen@gmail.com> - 2012-02-01 07:11 +0000
      Re: why bug in the program gwowen <gwowen@gmail.com> - 2012-02-01 00:14 -0800

#13682 — why bug in the program

Fromprava sahoo <spravasini@gmail.com>
Date2012-01-31 22:45 -0800
Subjectwhy bug in the program
Message-ID<3ad325e8-25e0-4f40-a145-d2faa35fe4ed@q8g2000pbb.googlegroups.com>
class foo
        {
                static int xxx;
                static int zzz;
        };
        static int xxx =2;
        int foo::xxx = 1;       // error here
        int foo::zzz = 1;
        static int zzz =2;

When compiled (gcc 2.4.5) It give an error for redefining xxx, but
not
for zzz,

because xxx is a static variable and its scope is inside the total
program so we can create only a single copy of this variable but zzz
in general variable inside class foo zzz is local to that class
foo .outside the class zzz is a variable of the program program .
these are consider two different variable with same name.

[toc] | [next] | [standalone]


#13683

FromMiles Bader <miles@gnu.org>
Date2012-02-01 16:02 +0900
Message-ID<buofwevrq7n.fsf@dhlpc061.dev.necel.com>
In reply to#13682
prava sahoo <spravasini@gmail.com> writes:
> When compiled (gcc 2.4.5) It give an error for redefining xxx, but not
> for zzz,

Do you really have gcc version 2.4.5?!  It was released about 20 years
ago!!!  :OOO

[Recent versions of gcc (4.4-4.7) don't give any error for your
example.]

-miles

-- 
Clarionet, n. An instrument of torture operated by a person with cotton in his
ears. There are two instruments that are worse than a clarionet -- two
clarionets.

[toc] | [prev] | [next] | [standalone]


#13684

FromGareth Owen <gwowen@gmail.com>
Date2012-02-01 07:11 +0000
Message-ID<87wr87do4y.fsf@gmail.com>
In reply to#13682
prava sahoo <spravasini@gmail.com> writes:

> class foo
>         {
>                 static int xxx;
>                 static int zzz;
>         };
>         static int xxx =2;
>         int foo::xxx = 1;       // error here

You've already declared foo::xxx to be an int (i.e. specified its type),
as part of the definition of class foo.  You cannot specify its type
again, and need only now set its value.

foo::xxx = 1; // the compiler already knows it's an int. 

>         static int zzz =2;

This isn't foo::zzz (a variable common to "class foo"), its an unrelated
file-local variable that happens to be called zzz. The keyword "static"
doesn't quite mean the same thing here as it does in the class
definition.

A more modern way would be to say:

namespace {
  int zzz = 2;
}

this will cause the compiler to give it a name like _mxyzptlk::zzz, so
we know that it isn't foo::zzz.  (There are other technical differences
w.r.t. visibility and linkage, but don't worry about those).

> (gcc 2.4.5)

That's a really, really old compiler...

[toc] | [prev] | [next] | [standalone]


#13685

Fromgwowen <gwowen@gmail.com>
Date2012-02-01 00:14 -0800
Message-ID<4e3a0836-b4cf-4010-9e38-e95ad68b377b@j15g2000yqb.googlegroups.com>
In reply to#13684
On Feb 1, 7:11 am, Gareth Owen <gwo...@gmail.com> wrote:

> foo::xxx = 1; // the compiler already knows it's an int.

Sorry, brain malfunction. Please please please ignore that.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web