Namespaces in C++
Namespaces..uhh?sounds like all alien stuff?? even for seasoned C++ players? let me explain...Now we all study in classes that C++ is an "OBJECT ORIENTED LANGUAGE", but actually strictly speaking C++ does not come under the category of OOP, but it is actually an OBJECT BASED LANGUAGE. Think again, do you always start a C++ program from inside a class? naa!! That is why. An OOP language should have everything in the form of objects , that is classes are mandatory, like in Java program. But hey, then we can do C++ programs exactly like a C++ program exactly like a C program, in the structered approachthat is why C++ is not exactly an OOP language.SO what's the big Deal??Now this OOP issue was haunting the ANSI(American National Standards Institute), who are the cheif players in C++. So they came up with the idea of NAMESPACES in around 1997. Now ,some other languages support it. Whole namespace bussiness is about keeping the global part of the program from getting polluted. Now if we have too many global variables in one or many files,(read header files), it will create many problems of duplicate declaration(or redefintions). To avoid this , they introduced a new level caled namespace. Now, anyhing declared global come under the global namespace. We can explicitly declare our programs inside a separate namespace, so that we can use same variable names or identifiers used in stadard headers,(yes can have a variable called cout). Now, ANSI has patched all the standard headers and reincorporated them into the namspace called the std namespace.But this is just come into existance and not extensively used , even now. But most new compilers support this technique. To maintain compatability with the old mode of programming, the following changes have been brought in. The headers with namespace does not have the conventional *.h but , sans any extension, ie
declaration:
namespace
{
//body //do anything!!,variables ,function,classes etc
}
I am not going into C++ details of using the namespaces in this episode, actually , i had that in mind , but the blogspot's html editor, thinks #include's tags and cout's insertion operators as html tags and creates errors, so i'll get back into that some time later..
In Closing...
Okay ,some may have convinced that namespaces are great, but then, is it not another face of classes itself!! Maybe not, definitely, namespaces avoid the global pollution. And you can use the standard names, they are not ANSI's private property anymore!! But , obviously it will take time before the namespaces are extensively used. Ahh, maybe this blog may speed that up!!
codevalley