Saturday, June 30, 2018

Why do you need namespace in a C++ program?

All identifiers in C++ must be unique and, if this rule is not observed in naming you end up with what is called 'naming resolution. The C++ compiler faces an ambiguous situation and, will throw up an error message.

Providing namespace is to help the compiler know that the identifier must be associated with a namespace. This is in much the same was as saying someone lives in Washington (Which one the State or D.C?). It is not a problem with small programs and it becomes a problem for larger programs.

In addition to identifier scope (local, global), namespace provides yet another option to reduce the name conflict in programs.

No comments: