private constants

A

anon

Hello

Can anyone tell me how to define constants private to a implementation
file. That is, I do not want to put them in a global namespace, but to
narrow their scope only to one implementation file.

Will this work:
static const int i=1;
?

Or is this better:

namespace
{
const int i=1;
}
?


If I do this:
const int i = 1;
am I going to pollute global namespace?


Thanks in advance.
 
V

Victor Bazarov

anon said:
Hello

Can anyone tell me how to define constants private to a implementation
file. That is, I do not want to put them in a global namespace, but to
narrow their scope only to one implementation file.

An anonymous namespace is usually recommended for making names appear
only in the current translation module.
Will this work:
static const int i=1;
?

Yes, certainly. Actually 'static' is superfluous in this case.
Or is this better:

namespace
{
const int i=1;
}
?

No, it's not better.
If I do this:
const int i = 1;
am I going to pollute global namespace?

Nope. A const that is not explicitly declared 'extern' has internal
linkage and hence unavailable from other modules.

V
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top