Why "using namespace" may cause problems

using this command, allows us to refer to methods later in the code, without writing namespace each time before these methods.
But there are many libraries and many namespaces within libraries. they all can contain same methods and thus, it would not be clear, which method (within which library or namespace) we would refer to later in the code.

I totally agree, I think this is discussed in the course. There are pros and ons, although overall I’m quite a fan of explicitly stating namespaces provided it doesn’t obfuscate the code too much.

Enjoy the course!

Thanks!

Hi people!!

So it is possible to declare a “main” or “general” namespace at the very begginig and if we find we want to use in a single case a method included in another namespace just declare it with “::” for this single case?

Supose we have std ans clash namespaces and both contains the cout class

Can I write something like

include

using namespace std;

int main () {
clash::cout << “Is this a crazy thing? \n”;
return 0;
}

Thank you!!

Hello Joan,

Yes, you can do it like that. But, the real question is : “Should I do it like that ?”, and the answer is no.

Why ? Because you can still have clashes with other functions, classes, etc… That are contained into the namespace. Moreover, if another developer come after you, he may not understant what you actually wanted to do, and may have multiple clashes when updating the code.

Privacy & Terms