+ 6

What is the use of name of namespaces in C++?

I wanted to know the use namespace from day 1. I thought why do we write "using namespace std;". I got the answer that it is so because we in c++ 11th standard, cout is written as std::cout. So to avoid writing it each time, we write so. I still never understood its proper use. Now, it is frequently asked to me in challenges. Can someone explain me the use of namespace?

11th Aug 2017, 3:52 AM
Harsh Kumar
Harsh Kumar - avatar
2 Answers
+ 11
You can define your own namespaces, and use them to identify what would be ambiguous function calls without them. Try modifying this code and see how 'using namespace' fits in. https://code.sololearn.com/ca0X1CRI8ZoS/?ref=app A different, perhaps clearer example though: #include <iostream> namespace one { void func() { std::cout << "test one"; } } namespace two { void func() { std::cout << "test two"; } } int main() { using namespace one; func(); return 0; }
11th Aug 2017, 4:55 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thanks Hatsy! 😊👍
11th Aug 2017, 5:35 AM
Harsh Kumar
Harsh Kumar - avatar