Jump to content

C++, std:: or using namespace


Ninjakreborn

Recommended Posts

Do you recommend doing:

#include <iostream>
using namespace std;

int main(void) {
   cout << "Test Text";
}

or

#include <iostream>

int main(void) {
  std::cout << "Test Text";
}

 

Throughout a variety of reading, I have been leaning towards std::cout instead of doing use namespace.  What is your personal preference?

 

What are the pros/cons of each?

I know you can setup your own namespaces:

#include <iostream>
using namespace std;

namespace mystuff{
int value = 5;
}

int main()
{
      cout << mystuff::value; //outputs 5
      return 0;
}

As of yet however, I have not found a really need to do this.

 

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c - This link has also gotten me doing some additional thinking. If it is bad practice to use "using namespace"

then "why" is it a bad practice to begin with. All in all since I have read it's bad practice I have been consistently using the std::cout way of doing things, but I have always wondered as to "why" it is considered more proper.

Link to comment
https://forums.phpfreaks.com/topic/233276-c-std-or-using-namespace/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.