CSC/ECE 517 Fall 2010/ch2 4d RB: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 77: Line 77:


In the above example, both the libraries are placed into different namespaces. This removes the ambiguity in the function calls. Each function can be referred uniquely using its namespace preceding it. Now it becomes to use the same names in different namespaces without any conflict. As long as the names are in different namespaces, each name will be unique because of the addition of the namespace identifier.
In the above example, both the libraries are placed into different namespaces. This removes the ambiguity in the function calls. Each function can be referred uniquely using its namespace preceding it. Now it becomes to use the same names in different namespaces without any conflict. As long as the names are in different namespaces, each name will be unique because of the addition of the namespace identifier.
=Namespaces in different languages=

Revision as of 01:42, 21 November 2010

Introduction

Namespaces are used in Computer Science to group similar items into different logical units, so that one item can be un-ambigously differentiated from another item. Trivial it may seem, but creating namespaces is one of the most basic activities of modern day programming. It makes a program more organized and less prone to errors.

Why namespaces

The Problem

In general programming languages like C++, variable names, function names, structure names, class names, union names and enumerations fall under one general category called names. While writing big programs involving several programmers, the situation is likely to go out of hand if proper control is not exercised on the visibility of these names. For example, lets consider the following:


//library1.h
char hello();
void print();
class EmpSal
{
  char* ename;
  int esal;
}
// library2.h
char hi();
void print();
class EmpSal
{
  char* nm;
  float tax;
}

In the above examples, we have two header files, library1.h and library2.h which have the function void print() and class EmpSal. Now if both the header files are included in a program (see below) and a reference to the class is made, or the function is called, then this situation becomes ambiguous.

//prog.c
#include "library1.h"
#include "library2.h"
void main()
{
 print();   // ambiguous call
 EmpSal es = new EmpSal(); // ambiguous reference EmpSal a class
}

We can see in the above code that a call to the method print() and creation of the object EmpSal es is ambigous. Since both the header files contain this method and this class, the compiler won't know which library is being referred here. In such a case, the compiler flashes an error message of name clash.

Possible solutions to this problem

The first solution can be to modify the name of the method and the class in one of the libraries. This will prevent a name clash and the code will compile correctly. But this is not a feasible solution. It might be a case, where the library is being referred in other parts of the program which we don't have access to. So making changes in the library will cause problems in other parts of the program. Secondly, the libraries may be "read-only" and we may not be allowed to change any part of the library. Thirdly, even if are able to make changes in the library and make our code run, this is not the best approach as this requires a lot of re-work and it may lead to potential bugs in the program.

The second, more appropriate approach can be to use the concept of namespaces and the libraries can be placed into specific namespaces. C++ by default provides a global namespace where all the class objects and methods are placed. The global namespaces can be split into further manageable pieces using the namespaces features in C++, Java, Python etc.

Lets take up an example of namespaces in C++ to understand this concept.

 //library1.h
namespace Library1
char hello();
void print();
class EmpSal
{
  char* ename;
  int esal;
}
// library2.h
namespace Library2
char hi();
void print();
class EmpSal
{
  char* nm;
  float tax;
}
using namespace std;
void main()
{
 Library1::print();
 Library2::printf();
 
 Library1::EmpSal es1;
 Library2::EmpSal es2;
}

In the above example, both the libraries are placed into different namespaces. This removes the ambiguity in the function calls. Each function can be referred uniquely using its namespace preceding it. Now it becomes to use the same names in different namespaces without any conflict. As long as the names are in different namespaces, each name will be unique because of the addition of the namespace identifier.

Namespaces in different languages