Global namespace pollution

B

Birt

An article states: the global namespace be polluted, causing "a namespace
collision."

What does this mean exactly? Thank you very much!
 
J

John Harrison

Birt said:
An article states: the global namespace be polluted, causing "a namespace
collision."

What does this mean exactly? Thank you very much!

This is OK.

namespace X
{
int g;
}
int g;


int main()
{
g = 0; // no problem
}


This is not.

namespace X
{
int g;
}
int g;
using namespace X; // pollution

int main()
{
g = 0; // which g?
}

In C++ I think the dangers of pollution are overstated, a bit like the real
world.

john
 
S

Sharad Kala

Birt said:
An article states: the global namespace be polluted, causing "a namespace
collision."

What does this mean exactly? Thank you very much!

When you write using namespace X, the names in that namespace are made visible
in the current scope.

Say,
namespace X{
class Y{
};
}
class Y{
};
int main(){
Y y; // OK
}

Next write using namespace X, so class Y gets introduced in the current scope.

namespace X{
class Y{
};
}
using namespace X;
class Y
{
};
int main{
Y y; // Error...Y is ambiguous
}

-Sharad
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top