What to look out for when using a namespace

Name spaces are convenient, and if nothing else, great pieces of code to save time; However, they do come at a small price.

The danger simply put is that the compiler will default to the namespace you have called, even though you may be referring to a method inside of a different library, resulting in the use of the wrong method.

As an example, the STD library, and one I may have written myself called ABC. My Library, ABC, contains a method called “Print” which I use when I want to tell a printer to blast ink onto a piece of paper. As we already know the STD library also contains a method called “Print” which is used for something else entirely.

If I have typed “using namespace std;” and later in my code want to have a printer blast ink onto a piece of paper, and simply write print, the compiler will assume I want to use the method from the std library.

2 Likes

As someone who is completely new to programming this explanation was great!

As a newblet, I appreciate this explanation.
So to get my printer to blast ink onto a page I would use

ABC::print;

Awesome. I thank you.

Privacy & Terms