Topic: Namespace scope question


Author: "John Romow" <holjar@masen.net>
Date: Thu, 25 Apr 2002 17:38:31 GMT
Raw View
Hi. Given the following code:

--- CODE ---
namespace N1 {
   int x;
};


using namespace N1;
int x; // Global 'x'

int main() {
   int x;

   return 0;
}

---END CODE---
Ok, in *this scenario*, to access the gloabl 'x', you would use:
   ::x
To get the x in the N1 namespace, yo could either use:
   N1::x
Or, in *this case*, simply:
   x

Now my question is, how can you access the x in main directly? when
using the "using namespace N1", the 'x' in 'N1' would be the one you get
when you refernce 'x' in main, right? So how does oen get the one in
main, short of declaring a pointer or reference to it? Or am I just
plain rong and the 'x' local to main really takes presedence? I'm sorry
I havent had an oppertunity to test this.

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                       ]





Author: Vincent Finn <1@2.com.cos.agilent.com>
Date: Sun, 28 Apr 2002 00:16:41 GMT
Raw View
The local scope comes before the namespace scope

x will give you the one in main (not the one in the namespace)
you MUST use N1::x inside main to get that version

John Romow wrote:

> Hi. Given the following code:
>
> --- CODE ---
> namespace N1 {
>    int x;
> };
>
> using namespace N1;
> int x; // Global 'x'
>
> int main() {
>    int x;
>
>    return 0;
> }
>
> ---END CODE---
> Ok, in *this scenario*, to access the gloabl 'x', you would use:
>    ::x
> To get the x in the N1 namespace, yo could either use:
>    N1::x
> Or, in *this case*, simply:
>    x
>
> Now my question is, how can you access the x in main directly? when
> using the "using namespace N1", the 'x' in 'N1' would be the one you get
> when you refernce 'x' in main, right? So how does oen get the one in
> main, short of declaring a pointer or reference to it? Or am I just
> plain rong and the 'x' local to main really takes presedence? I'm sorry
> I havent had an oppertunity to test this.
>
> 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                       ]

---
[ 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                       ]