Namespaces in Regards to Files in C++

H

Hal Vaughan

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.cpp"

(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
 
V

Victor Bazarov

Hal said:
[...]
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?

Put your variable in an anonymous namespace or declare it 'static'
in the global namespace. 'static' outside of a function or a class
makes linkage internal. Anonymous namespace adds something unique
for that file.

namespace { // anonymous
int myvariable = 42;
}

void foo() {
myvariable = 777;
}

void bar() {
myvariable = 666;
}


V
 
H

Hal Vaughan

Victor said:
Hal said:
[...]
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?

Put your variable in an anonymous namespace or declare it 'static'
in the global namespace. 'static' outside of a function or a class
makes linkage internal. Anonymous namespace adds something unique
for that file.

namespace { // anonymous
int myvariable = 42;
}

void foo() {
myvariable = 777;
}

void bar() {
myvariable = 666;
}

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.

Thanks!

Hal
 
V

Victor Bazarov

Hal said:
[..]
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
 
H

Hal Vaughan

Victor said:
Hal said:
[..]
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
 
J

Jeff Schwab

Hal said:
Victor said:
Hal said:
[..]
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.
 
H

Hal Vaughan

Jeff said:
Hal said:
Victor said:
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
 
A

Anand Hariharan

)
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.
(...)
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.
(...)
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?

While others have given you the answer that you sought, I have a
counter question for you. Despite all that you have heard regarding
variables visible across files or even across functions within the
same file, why would you still want to define such variables?

Why not put these variables as private members of a class, and the
specific functions that need them, as member functions of the same
class? For all you come to realise, the class might actually serve
to abstract something meaningful to your project.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top