tags:

views:

44

answers:

2

Hi, I have few questions (keeping C/C++ in context) - When I want to use namespace? - When I want to use classes inside namespace? - When I should use functions/routines inside namespace (not inside class)?

Thanks.

+2  A: 

When I want to use namespace?

You should use a namespace whenever it helps to organize your code better. You can group related classes, functions, constants, and types together under a single namespace.

When I should use functions/routines inside namespace (not inside class)?

Whenever the function does not need to be a member function. Namely, if a function does not need to access any member variables of any class instance, it should be implemented as a non-member function.

Herb Sutter's Guru of the Week articles What's In a Class? -- The Interface Principle and Namespaces and the Interface Principle discuss the use of non-member functions and namespaces in an interface.

James McNellis
A: 

Namespaces provides a way to organize your code, which it is useful for. Namespaces does not provide any extra functionality to C++. If used poorly it tends to the code more complicated to debug. If you decide to use namespaces, try avoid putting "using namespace" in your source files. Never use them in your header files. Point out specific classes instead "using std::vector" or similar, or add the use of "using namespace" inside a scope. This will make all your future use of namespaces much less painful.

Simon