Namespaces in Regards to Files in C++

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

    Namespaces in Regards to Files in C++

    I feel like I should know this or be able to find the answer, but I can't.
    I've read a few tutorials or articles on namespaces, but this applies to
    files and I don't see much on namespaces in regard to files.

    When I first started working on my project (this is my "initiation " project
    in C++), I had one .cpp file with definitions that were more or less global
    (yes, I can hear the jaws dropping now). I was only using this while I was
    working out a number of details and now everything is divided up and there
    are no global variables.

    While I was using this file of variables, I did

    #include "varfile.cp p"

    (Okay, *now* I know there's many problems with that, so I don't need to hear
    it was wrong!)

    When I did that I expected to have access in one source file to the
    variables in that file. Now I've cleaned it up. I use some routines
    across files, but I handle that with making a .h file for each source file
    and including the function prototypes. No variables are used in any .h
    files.

    What has me confused is that I thought if I had a variable name in use in
    one file, it was specific to that file and all the files had their own
    namespace. That doesn't seem to be so. I'm using make and g++ to compile
    my program, but even without make, if I compile the .o files for each
    source file, when I try the final compilation, I get errors that some
    variables are defined in two separate files.

    I want to have variables that are used only in the file they're defined in,
    but I also want to be able to use some of the functions in that same file
    that use that variable. I thought everything in a file was in its own
    namespace by default and other files would not know about it unless I put
    it in an include file.

    Obviously that's wrong.

    So how do I define a variable in one source file that others won't see and
    still be able to access the functions in that file?

    Thanks!

    Hal
  • Victor Bazarov

    #2
    Re: Namespaces in Regards to Files in C++

    Hal Vaughan wrote:
    [..]
    So if it's in an anonymous namespace, the local file can use it but
    other files cannot? I think that's what you're saying, but I just
    want to make sure I'm clear.
    There is no way for the code in other files to get to that object,
    although it does have an externally accessible name. The code in
    the other modules will never figure out how to refer to that var.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Hal Vaughan

      #3
      Re: Namespaces in Regards to Files in C++

      Victor Bazarov wrote:
      Hal Vaughan wrote:
      >[..]
      >So if it's in an anonymous namespace, the local file can use it but
      >other files cannot? I think that's what you're saying, but I just
      >want to make sure I'm clear.
      >
      There is no way for the code in other files to get to that object,
      although it does have an externally accessible name. The code in
      the other modules will never figure out how to refer to that var.
      Great. I did read on how anonymous namespaces were handled by the compiler
      (by it generating a more or less random name), but I wasn't clear that
      something in one could still be accessed by everything else in that
      particular file (unless other namespaces were used).

      I always ask to be sure because it seems like there are so many effects in
      C++ that aren't always obvious.

      Thanks!

      Hal

      Comment

      • Jeff Schwab

        #4
        Re: Namespaces in Regards to Files in C++

        Hal Vaughan wrote:
        Victor Bazarov wrote:
        >
        >Hal Vaughan wrote:
        >>[..]
        >>So if it's in an anonymous namespace, the local file can use it but
        >>other files cannot? I think that's what you're saying, but I just
        >>want to make sure I'm clear.
        >There is no way for the code in other files to get to that object,
        >although it does have an externally accessible name. The code in
        >the other modules will never figure out how to refer to that var.
        >
        Great. I did read on how anonymous namespaces were handled by the compiler
        (by it generating a more or less random name), but I wasn't clear that
        something in one could still be accessed by everything else in that
        particular file (unless other namespaces were used).
        Nit: The namespace contents are visible only below their declarations.
        For example,

        namespace { struct s; }
        extern s* p;

        compiles, whereas

        extern s* p;
        namespace { struct s; }

        does not.

        Comment

        • Hal Vaughan

          #5
          Re: Namespaces in Regards to Files in C++

          Jeff Schwab wrote:
          Hal Vaughan wrote:
          >Victor Bazarov wrote:
          >>
          >>Hal Vaughan wrote:
          >>>[..]
          >>>So if it's in an anonymous namespace, the local file can use it but
          >>>other files cannot? I think that's what you're saying, but I just
          >>>want to make sure I'm clear.
          >>There is no way for the code in other files to get to that object,
          >>although it does have an externally accessible name. The code in
          >>the other modules will never figure out how to refer to that var.
          >>
          >Great. I did read on how anonymous namespaces were handled by the
          >compiler (by it generating a more or less random name), but I wasn't
          >clear that something in one could still be accessed by everything else in
          >that particular file (unless other namespaces were used).
          >
          Nit: The namespace contents are visible only below their declarations.
          For example,
          >
          namespace { struct s; }
          extern s* p;
          >
          compiles, whereas
          >
          extern s* p;
          namespace { struct s; }
          >
          does not.
          It's not a nit, that's pretty important.

          Thanks!

          Hal

          Comment

          Working...