Namespaces don’t really

  Namespaces don’t really fit into the functions category, but they are an important concept that we need to cover before we get to classes and object oriented programming, and this is really as good a place as any.

  Pretend you are the teacher of a classroom of students. For sake of example, let’s say there are two boys named “Alex”. If you were to say, “Alex got an A on his test”, which Alex are you referring to? Nobody knows, unless you have a way to disambiguate which Alex you mean. Perhaps you point at one, or use their last names. If the two Alex’s were in different classrooms, there wouldn’t be a problem — the problem is really that there are two things with the same name in the same place. And in fact, as the number of students in the classroom increase, the odds of having two students with the same first name increases exponentially.

  A similar issue can arise in programming when two identifiers (variable and/or function names) with the same name are introduced into the same scope. When this happens, a naming collision will result, and the compiler will produce an error because it does not have enough information to resolve the ambiguity. As programs get larger and larger, the number of identifiers increases linearly, which in turn causes the probability of naming collisions to increase exponentially.

  Let’s take a look at an example of a naming collision. In the following example, foo.h and goo.h are the header files that contain functions that do different things but have the same name and parameters.

  foo.h:

  1

  2

  3

  4

  5// This DoSomething() adds it's parameters

  int DoSomething(int nX, int nY)

  {

  return nX + nY;

  }

  goo.h:

  1

  2

  3

  4

  5// This DoSomething() subtracts it's parameters

  int DoSomething(int nX, int nY)

  {

  return nX - nY;

  }

  main.cpp:

   10#include

  #include

  #include

  int main()

  {

  using namespace std;

  cout << DoSomething(4, 3); // which DoSomething will we get?

  return 0;

  }

  If foo.h and goo.h are compiled separately, they will each compile without incident. However, by including them in the same program, we have now introduced two different functions with the same name and parameters into the program, which causes a naming collision. As a result, the compiler will issue an error:

  c:\\VCProjects\\goo.h(4) : error C2084: function 'int __cdecl DoSomething(int,int)' already has a body

  In order to help address this type of problem, the concept of namespaces was introduced.

  What is a namespace?

  A namespace defines an area of code in which all identifiers are guaranteed to be unique. By default, all variables and functions are defined in the global namespace. For example, take a look at the following snippet:

  1

  2

  3

  4

  5int nX = 5;

  int foo(int nX)

  {

  return -nX;

  }

  Both nX and foo() are defined in the global namespace.

  In the example program above that had the naming collision, when main() #included both foo.h and goo.h, the compiler tried to put both versions of DoSomething() into the global namespace, which is why the naming collision resulted.

  In order to help avoid issues where two independent pieces of code have naming collisions with each other when used together, C++ allows us to declare our own namespaces via the namespacekeyword. Anything declared inside a user-defined namespace belongs to that namespace, not the global namespace.

  Here is an example of the headers in the first example rewritten using namespaces:

  foo.h:

發佈了75 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章