namespace and global scope

Discussion in 'C++' started by Wat, Dec 29, 2004.

  1. Wat

    Wat Guest

    If a class is not given a namespace, what does this imply?

    From inside of the class, if to call global functions, should global scope
    :: be used at all?

    If global scope :: used, what benefits does it bring?

    Thanks in advance!
    Wat, Dec 29, 2004
    #1
    1. Advertising

  2. Wat wrote:
    > If a class is not given a namespace, what does this imply?


    What do you mean by "given a namespace"? If it is declared outside of
    any namespace, it is declared in the global namespace.

    > From inside of the class, if to call global functions, should global scope
    > :: be used at all?


    Only to resolve name conflicts or for readability of the code.

    > If global scope :: used, what benefits does it bring?


    The code becomes a bit more readable if nothing else.

    V
    Victor Bazarov, Dec 29, 2004
    #2
    1. Advertising

  3. On Wed, 29 Dec 2004 11:49:43 -0500, Victor Bazarov
    <> wrote:

    >Wat wrote:
    >> If a class is not given a namespace, what does this imply?

    >
    >What do you mean by "given a namespace"? If it is declared outside of
    >any namespace, it is declared in the global namespace.
    >
    >> From inside of the class, if to call global functions, should global scope
    >> :: be used at all?

    >
    >Only to resolve name conflicts or for readability of the code.
    >
    >> If global scope :: used, what benefits does it bring?

    >
    >The code becomes a bit more readable if nothing else.


    It improves *maintainability*, but not necessarily just by improving
    readability. It prevents name-lookup in other namespaces which might
    not have been visible when the implementation was first written.

    For example, I worked on a project once (in OS Windows) which involved
    the open source library ungif. ungif (a library implemented in C
    language, mostly in Unix/Linux operating systems) has a function
    called "DrawText". C doesn't have namespaces. Of course, Windows API
    (also written in C) also has a function called "DrawText". Hmmm ...
    what did we do?

    We changed the source of ungif so that if __cplusplus was defined, we
    put "DrawText" into a namespace "ungif::DrawText". Worked out very
    nicely...But what if we had tried to put the entire header into a
    namespace?

    That would also have been a possible solution. But then we have the
    problem that someone might have placed a "using namespace ungif;"
    somewhere in a file which also called the Win32API function
    "DrawText".

    You see where I am going to...?

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 29, 2004
    #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. Paul Opal
    Replies:
    12
    Views:
    946
    Paul Opal
    Oct 11, 2004
  2. Anonymous
    Replies:
    3
    Views:
    531
    Ron Natalie
    Aug 18, 2003
  3. Steven T. Hatton
    Replies:
    9
    Views:
    477
  4. Stefan Behnel
    Replies:
    2
    Views:
    191
    Gregory Ewing
    Aug 16, 2011
  5. Andrew Falanga
    Replies:
    2
    Views:
    201
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page