question on namespace

Discussion in 'C++' started by Milan, Aug 5, 2003.

  1. Milan

    Milan Guest

    Is there any difference between:

    a) using namespace std;
    namespace MyNamespace
    {
    : // my own code
    }

    b) namespace MyNamespace
    {
    using namespace std;
    : // my own code
    }


    I found some of my code do not compile if (b) is used. Why?

    Regards,
    Milan.
     
    Milan, Aug 5, 2003
    #1
    1. Advertising

  2. "Milan" <> wrote...
    > Is there any difference between:
    >
    > a) using namespace std;
    > namespace MyNamespace
    > {
    > : // my own code
    > }
    >
    > b) namespace MyNamespace
    > {
    > using namespace std;
    > : // my own code
    > }


    Yes, there is. The latter case brings symbols defined in
    'std' namespace into 'MyNamespace'. The former brings them
    into the namespace that contains 'MyNamespace', probably the
    global one.

    >
    >
    > I found some of my code do not compile if (b) is used. Why?


    Hard to say without seeing your code.

    Victor
     
    Victor Bazarov, Aug 5, 2003
    #2
    1. Advertising

  3. Milan

    Fogus Guest

    > b) namespace MyNamespace
    > {
    > using namespace std;
    > : // my own code
    > }
    >
    >
    > I found some of my code do not compile if (b) is used. Why?



    Because the using directive for std is restrained within the MyNamespace
    scope. If you try to access any std entities outside of MyNamespace
    as if they were global (i.e. as if you had used a using std at the
    top-level scope), then they will fail because the cannot 'see' inside.

    -m
     
    Fogus, Aug 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    592
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,156
  3. Anonymous
    Replies:
    3
    Views:
    554
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    462
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,418
    Howard
    Nov 29, 2004
Loading...

Share This Page