"Tracey" <
XXXX@XXXXX.COM >writes:
Quote
Is it [better/best/irrelevant] to put the following [BEFORE] main (and maybe
before prototypes), but [AFTER] the includes instead of [IN] main:
using std::cout;
using std::cin;
using std::endl;
The text book I'm using has:
int main()
{
using std::cout;
using std::cin;
using std::endl;
...
The difference is just the scope of the using directive. At file
scope, everything in the file is "using" std::cout (and others), but
when it's in main, only main is using the name, and code outside of
main must still qualify the names.
You may think that it's better to use the names at file scope, because
more code (the whole file) can take advantage of it. Others might
argue that it's worse for the same reason.
The entire point of namespaces in C++ is to partition the names into a
hierarchy, to avoid collisions and conflicts. When you use a
namespace, or use names from a namespace, you're flattening the
hierarchy, and increasing the chance of there being a naming
collision. It is usually not a problem, but when it actually is a
problem, it can be incredibly subtle--such that sometimes you might
not even notice you have a problem, but worse, you have a problem and
don't know what it is.
I like to restrict the scope of using directives to the smallest scope
possible for them to still be valuable, if I use them at all. (It is
sometimes very convenient, but never necessary.)
--
Chris (TeamB);