Discussion:
Global namespace pollution
(too old to reply)
Birt
2004-05-28 06:36:07 UTC
Permalink
An article states: the global namespace be polluted, causing "a namespace
collision."

What does this mean exactly? Thank you very much!
John Harrison
2004-05-28 06:43:29 UTC
Permalink
Post by Birt
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
Sharad Kala
2004-05-28 06:52:06 UTC
Permalink
Post by Birt
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

Loading...