0
What is a namespace in C++?
I know why I should use 'using namespace std;' statement in almost every C++ programs. But what exactly is a namespace? Please explain concisely. Thanks
1 Antwort
+ 1
It's a way to avoid ambiguous names. If you have two functions both called TestFunc(); then the compiler would not know which one to use. A namespace gives an extra identifier to tell the compiler which function to use. If you define your own function that happens to have the same name as another in a different library, you can't do it.
Since you now know a namespace gives you an extra identifier, if the function you want to use exists in a particular namespace (like standard (std)), then you can put std:: before the function to tell the compiler "I want to use the version of this function that exists in the standard namespace".