Click to See Complete Forum and Search --> : namespace
mickey0
April 6th, 2008, 01:10 PM
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.
Hermit
April 6th, 2008, 01:42 PM
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.
mickey0
April 6th, 2008, 01:56 PM
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?
STLDude
April 6th, 2008, 02:02 PM
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.
namespace bigcorporation
{
class Object
{
// blah, blah
};
}
Now, if I'm using this library, but I also have class name Object
namespace littleguy
{
class Object
{
// blah, blah
};
}
then later in my 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.
mickey0
April 6th, 2008, 04:32 PM
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?
STLDude
April 6th, 2008, 04:46 PM
Yes.
JohnW@Wessex
April 7th, 2008, 08:22 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.