Same Artifact Name, Different Namespace Qualifiers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Same Artifact Name, Different Namespace Qualifiers

This question is primarily for C++ when invoking an artifact (ie. cout; cin). When an artifact is invoked from its' respective namespace ([namespace name]), the requirement of declaration is by either invoking the 'using namespace [namespace name];' or 'using [namespace name]::[artifact];'. What if you have a scenario where there exist artifacts with the exact same names and resides in different namespaces in which you would like to invoke both artifacts in the same function block, how would you go about doing this? Maybe this is something that does not happen and I am jumping ahead of myself, but is something that has crossed my mind while learning this particular programming concept.

3rd Dec 2016, 5:02 PM
Bryan Isles
Bryan Isles - avatar
1 Answer
0
So instead of adding "Using namespace std;" in your code, you could just use all your declarations as follows: std::cout << "Please enter your name\n"; std::cin >> sName Then if for whatever reason, you felt that you wanted to add a function called cout in a namespace called foobar, you could use it as follows: std::cout << "Please enter your name\n"; std::cin >> sName; {more code here...} foobar::cout("Whatever") ============ There's a very good explanation as to why using the namespace std is considered bad practice on Stack Overflow if you are interested in learning more. stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
16th Dec 2016, 7:35 PM
Frédéric Charette
Frédéric Charette - avatar