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


Groups > comp.lang.c++ > #13684

Re: why bug in the program

From Gareth Owen <gwowen@gmail.com>
Newsgroups comp.lang.c++
Subject Re: why bug in the program
References <3ad325e8-25e0-4f40-a145-d2faa35fe4ed@q8g2000pbb.googlegroups.com>
Date 2012-02-01 07:11 +0000
Message-ID <87wr87do4y.fsf@gmail.com> (permalink)

Show all headers | View raw


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...

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web