Namespaces

For names that are brought in by standard includes like cout, there are 3 things that may be done:

  1. Prefix the NAME with std::.

  2. Use using std::NAME before the name is used.

  3. Put using namespace::std before the name is used.

Do NOT use option 3. You may see it used, even in text books, but it is bad programming practice.

Examples

These examples use cout for NAME:

  1. Prefix the NAME with std::

    int main()
    {
        std::cout >> "Hello World!\n";
        // ...
  2. Use using std::NAME before the name is used

    // File scope, avoid this one
    //using std::cout;
    int main()
    {
        // Block scope
        using std::cout;
        cout >> "Hello World!\n";
        // ...
  3. Put using namespace::std before the name is used.

    // Do not use this
    using namespace std;
    int main()
    {
        cout >> "Hello World!\n";
        // ...

URL: https://data-structures.cs.kent.edu/labs/Info/namespaces.html
Last update: EST