-
namespace
Hello,
recently I'm wondering why to use "namespaces". The most important aim I founded is: if 2 person write code and after they fusion it, there'll can be collision names; so putting code in two different namespaces could save the programmers from change the "name" of varibles in their code (code that could be very large). But after it I wondered too: if I do "very object" programming, all it's an object; so the names inside an object can't collides....So I'm thinking namespace are only a C++ "escamotage" for helps to integration with old C code and to help old C programmer too; is that?
thanks.
-
Re: namespace
Namespaces are to avoid naming collisions, that's all (in saying that I'm not forgetting about anonymous namespaces). It doesn't matter how object-oriented your code is, because class names can collide just like anything else.
-
Re: namespace
object names can collide, yes, but does make sense to have in the same project two or more class with the same name? in this happen, isn't that someone has done the work of others?
-
Re: namespace
How many times you see third party library/sdk's using name for classes like Object, Actor, Entity, Manager, Vector (or even worse vector). By using namespace these names still can be retained, but wrapped in.
Code:
namespace bigcorporation
{
class Object
{
// blah, blah
};
}
Now, if I'm using this library, but I also have class name Object
Code:
namespace littleguy
{
class Object
{
// blah, blah
};
}
then later in my code
Code:
littleguy::Object smallFoo = littleguy::Object;
// and
bigcorporation::Object bigFoo = bigcorporation::Object;
Now, I'm not advocating using Object as a class name, but only for example purposes here.
-
Re: namespace
Are you mean that project where class name collides shouldn't be occur, but they can. So we can save ourself from this with namespace. Is this?
-
Re: namespace
-
Re: namespace
They can also be used to partition your functions and classes in to logically related sections.
i.e.
MyLibrary::Timers::Timer timer;
MyLibrary::Gui::Charts::PieChart chart1;
MyLibrary::Gui::Charts::BarChart chart2;
This can be quite handy when you use an IDE like Visual Studio as the intellisense feature allows you to find the class or function you require very quickly.