A namespace alias is similar in nature to a typedef. A namespace alias enables you to
create another name for a named namespace. This can be quite useful when you are
using a namespace with a long name or nested namespaces.
Every file can contain an unnamed namespace. An unnamed namespace, as its name
implies, is a namespace without a name. An unnamed namespace allows you to use the
names within the namespace without qualification. It keeps the namespace names local
to the translation unit. Unnamed namespaces are the same as declaring a global variable
with the statickeyword.
Q&A ....................................................................................................................
Q Do I have to use namespaces?
A No, you can write simple programs and ignore namespaces altogether. Be certain
to use the old Standard Libraries (for example,#include <string.h>) rather than
the new libraries (for example,#include <cstring>. Because of the reasons you
learned in today’s lesson, it is recommended that you do use namespaces.
Q Is C++ the only language that uses namespaces?
A No. Other languages also use namespaces to help organize and separate values.
This includes languages such as Visual Basic 7 (.NET), C#, and more. Other lan-
guages have similar concepts. For example, Java has packages.
Q What are the unnamed namespaces? Why do I need unnamed namespaces?
A Unnamed namespaces are namespaces without names. They are used to wrap a col-
lection of declarations against possible name clashes. Names in an unnamed name-
space cannot be used outside of the translation unit where the namespace is
declared.
Workshop ............................................................................................................
The Workshop contains quiz questions to help solidify your understanding of the mater-
ial covered and exercises to provide you with experience in using what you’ve learned.
Try to answer the quiz and exercise questions before checking the answers in Appendix
D, and be certain you understand the answers before going to tomorrow’s lesson.
Quiz ................................................................................................................
- How do you access the function MyFunc()if it is in the Innernamespace within
the Outernamespace?
Outer::Inner::MyFunc();
656 Day 18