About namespace statements?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itiger
    New Member
    • Sep 2010
    • 12

    #1

    About namespace statements?

    Does the Namespace Statments for e.g.
    Code:
    using namespace std;
    causes the compiler to load anything additional into the working memory. Does it has any effect on programs execution.
    Or it only allows you to use the short class names in source program.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    namespaces are tp prevent collisions when yu use names that are the same as someone else's. All names need to be unique.

    Suppose you have a variable called cout. That would conflict with the cout that's part of C++ so you put your variable in a namespace:

    Code:
    namespace MyStuff
    {
       int cout;
    }
    Now you can code:

    Code:
    MyStuff::cout = 10;
    and still use the C++ cout:

    Code:
    std::cout << MyStuff::cout << std::endl;
    The preferred technique is to precede each variable with it's namespace as above.

    The next worst thing to do is put a "using" declaration at the beginning of each function. But that may not resolve collisions within that function.

    The next worst thing to do is to place a "using" declaration at the beginning of the file since that would permit collisions to all functions in that file.

    And the biggest no-no of all is to put the "using" declaration in a header file that is included all over the place. That would permit collisions in all of your code.

    Comment

    Working...