![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 56
| Why std:: and not using namespace std? |
| swappo is offline | |
| | #2 |
| Student Join Date: Aug 2008 Location: UK -> Newcastle
Posts: 156
| You use the resolution scope operator wherever you use a member of namespace std. Yes it is a lot of extra typing, but it makes sure that you want to use that particular member of std rather than a user defined member of something else. Say you want to declare a string variable from the std namespace: Code: std::string sSomeVar; Code: class string
{
/*Class members here*/
};
string sMyStringObject;
Code: using namespace std; string sSomeVar; /*String from std namespace*/ string sMyStringObject; /*Object of class*/ /*Compiler doesn't know what string class to pick!*/
__________________ MSDN <- Programmers Haven! |
| legit is offline | |
| | #3 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > When is it acceptable to use using namespace std? As a hack to get old code up and running. > I was recently told that I should use std:: in my header files as opposed to using namespace std but I don't really know why. Because if someone else uses your header, then they get the whole namespace whether they like it or not. And there's no way to undo the damage, so they have to come to you to edit the code. > Should I be using std:: in .cpp files as well? There you have more choice. But you might consider say using std::cout; if you happen to be using cout a lot, and use the std:: prefix for everything else. If you're working on your own, there isn't a lot in it. But if you end up working in large teams, with many namespaces flying about, then knowing how to be strict about such things is a big help.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #4 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |||
| laserlight is online now | |
| | #5 |
| Registered User Join Date: Jun 2005
Posts: 1,343
| "using namespace std;" can introduce ambiguity in any code that employs named objects or functions from multiple namespaces. The result of that ambiguity is code that cannot compile, or code that compiles and gives the wrong results, and the effects cannot be undone. The only fix is to prefix the affected names with the namespace name (i.e. to use use "std::<name>" or "conflicting_namespace::<name>" syntax) or to modify the code to eliminate "using namespace" directives. The second option is impossible for commercial libraries distributed without source (i.e. in the form of header files and compiled libraries). Legally and contractually, "using namespace std;" in a header file can render a commercial library unfit for its stated purpose. The sad thing is that quite a few text books employ "using namespace std;" in all their examples, with a strong implication that is good practice. It is anything but. |
| grumpy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|