Topic: static member has global scope?????
Author: qazmlp1209@rediffmail.com (qazmlp)
Date: Thu, 25 Jul 2002 00:53:08 GMT Raw View
"Victor Bazarov" <vAbazarov@dAnai.com> wrote in message news:<2Xo%8.14630$uh7.3623@sccrnsc03>...
> "qazmlp" <qazmlp1209@rediffmail.com> wrote...
> > I have a class declared in a header file & it has a static member.
> > This header will be included in multiple .cpp files.
> > //common.h
> > class test
> > {
> > private:
> > static int VarIntSt;
> > };
> >
> >
> > When I define this static member 'VarIntSt' in multiple .cpp files
> > which will be compiled and then linked together, I'm getting Multiple
> > definition error. So, I inferred that unlike the static non-class
> > variable which has file scope, static class member is actually global.
>
> No. You're violating one definition rule. There shall be only
> one definition of any object with external linkage.
>
> Static members of a class have external linkage if the name of
> the class has external linkage (and it usually does).
>
> Non-static data members do not have linkage.
>
> > Am I correct? If yes, can anybody gimme the proof of it from the C++
> > standard draft? And also, can you please explain why is it like that?
>
> All "why" questions should be addressed to comp.std.c++. They
> explain why. We only try our best at giving the answers "how".
>
> > Isn't there a necessity to have the file scope even for a static class
> > member also?
>
> I am not sure what you mean here.
>
> Pick a translation unit and place the definition there. Remove
> the definition from the header.
>
> What you did was putting the definition in every translation unit
> by the means of including the code from the header into the units.
According to Victor's advice in comp.lang.c++, I'm forwarding the mail
to comp.std.c++.
Thanks!
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]