|
-
April 6th, 2008, 01:10 PM
#1
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.
-
April 6th, 2008, 01:42 PM
#2
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.
- Alon
-
April 6th, 2008, 01:56 PM
#3
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?
-
April 6th, 2008, 02:02 PM
#4
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.
-
April 6th, 2008, 04:32 PM
#5
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?
-
April 6th, 2008, 04:46 PM
#6
-
April 7th, 2008, 08:22 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|