namespace and global scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wat

    #1

    namespace and global scope

    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!


  • Victor Bazarov

    #2
    Re: namespace and global scope

    Wat wrote:[color=blue]
    > If a class is not given a namespace, what does this imply?[/color]

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

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

    The code becomes a bit more readable if nothing else.

    V

    Comment

    • Bob Hairgrove

      #3
      Re: namespace and global scope

      On Wed, 29 Dec 2004 11:49:43 -0500, Victor Bazarov
      <v.Abazarov@com Acast.net> wrote:
      [color=blue]
      >Wat wrote:[color=green]
      >> If a class is not given a namespace, what does this imply?[/color]
      >
      >What do you mean by "given a namespace"? If it is declared outside of
      >any namespace, it is declared in the global namespace.
      >[color=green]
      >> From inside of the class, if to call global functions, should global scope
      >> :: be used at all?[/color]
      >
      >Only to resolve name conflicts or for readability of the code.
      >[color=green]
      >> If global scope :: used, what benefits does it bring?[/color]
      >
      >The code becomes a bit more readable if nothing else.[/color]

      It improves *maintainabilit y*, 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::DrawTex t". 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
      NoSpamPlease@Ho me.com

      Comment

      Working...