0

what is namespace?

18th Jun 2016, 7:04 PM
souvik singha
souvik singha - avatar
2 Answers
0
A namespace is a means to define the context of your models (these are your classes, structs functions etc.) Sometimes this is really helpful as models from different contexts but with the same name have to be modeled in the software. An example: namespace music { class bow; } namespace weaponry { class bow; } namespace london { class bow; } // to refer for use, now write: music::bow, weapon::bow... All three "bows" are different in meaning in what they describe (utility for string instruments, ancient / medieval weapon, city district of London) and therefore very likely differ in their declaration and implementation. Yet, the same word is used to express them. Without namespaces, one had to make the names of the classes unique. Without namespaces, this would make the names clunkier (music_bow, weapon_bow, etc) or less descriptive (bow1, bow2, bow3) in their context. Btw, by not wrapping a class in an explicit namespace declaration as shown in the example, the class, struct, function etc. is put in the default namespace. Therefore *all* classes, structs, functions are in a namespace. More generally, this means about the uniqueness of names that they have to be unique *in a namespace*, actually (no multiple classes "bow" allowed in music, weaponry or london...) Btw, if you wondered why we don't have to write a :: in front of the stuff in the default namespace... It's a short-cut, you can actually do it and as the default namespace has an empty name, you could refer to music::bow by ::music::bow and all the other elements in the default namespace by pretending ::, too.
19th Jun 2016, 1:38 AM
Stefan
Stefan - avatar
0
As the answer had to touch some topics that might not be straightforward to understand, pls let me know if I can improve on some parts to help you understand.
19th Jun 2016, 10:45 AM
Stefan
Stefan - avatar