473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.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
Mar 20 '08 #1
4 1259
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
Mar 20 '08 #2
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
Mar 20 '08 #3
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.
Mar 20 '08 #4
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
Mar 20 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rubén Campos | last post by:
Organizing classes, types, structures, enums and whatever other entities into nested namespaces requires to include into every header and implementation file the complete path of namespaces. Let me...
6
by: Michael C | last post by:
In Vb a class was a file, but in C# you have multiple classes in one file which has a namespace. I am trying to get some common code libraries written but this is stuffing my head up. Is it...
9
by: Patty O'Dors | last post by:
Hi Can somebody please tell me what namespaces are actually for? I notice that when I start a new project in C#, it puts everything in a namespace of the same name as the project. I found them...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.