Best Answer

There are many situations when writing a computer program that requires one to make use of libraries provided by other people. Suppose that you have created a program to do matrix multiplication, and one of your functions inside this program is called multMatrix(a, b). Now suppose you are using a library provided by someone else (not necessarily for matrices), and quite by coincidence their library also contains a function called multMatrix(a, b) -- thus both of these functions have exactly the same signature. Which one should be used? Old-school programmers and librarians used to go through a lot of effort to try and establish function names that would be unique -- however, this did not always solve the problem. The use of name-spaces provides an adequate mechanism for avoiding these "name clashes". Functions (or methods) are now grouped into namespaces, and we are assured (to a certain point) that namespaces should be unique (for example if you use the name of the company you are working for as a namespace), now even if two functions have the same signature they should be located within different namespaces, allowing you to avoid name-clashes.

User Avatar

Wiki User

โˆ™ 2006-10-13 17:00:18
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide

Add your answer:

Earn +20 pts
Q: What is the use of a namespace?
Write your answer...
Related questions

What is the use of using namespace STD in c plus plus programming?

The using namespace std statement identifies the default namespace when it is not explicitly stated in the code. If you did not specify a namespace, simple things like cout and endl would have to be specified as std::cout and std:endl.

What is namespace in c plus plus program?

A namespace is similar to a class in object oriented programming. A namespace contains functions defined by the programmer. for example namespace std contains functions like cout and cin.namespaces can be globaly declared like so: "using namespace std;"which includes all the functions located in the namespace std.if you only need to use cout you can globaly declare only cout like this "using std::cout;"orstd::cout

Which namespace of the NET Framework do you use to create metadata dynamically at runtime?


In c plus plus Programming can you replace STDio header with using name space STD?

No. You can't use namespace std even if you include stdio.h. At the very least you must include stddef.h before you can use namespace std.

What is the use of namespace?

Code is divided up into namespaces to keep unrelated code elements separate from each other. Namespaces can also contain other namespaces, thus we can create a hierarchy of namespaces to logically group different namespaces under a common name. The global namespace is an anonymous namespace (it has no name) and all user-defined namespaces exist within this global namespace. Thus the global namespaces serves as the root of all namespace hierarchies. When we wish to refer to a name within a namespace outwith the scope of the current namespace, we must use the scope resolution operator. For instance: void my_function () {/*...*/} namespace my_code { void my_function () {/*...*/} }; The my_function name is defined in the global namespace whereas my_code::my_function refers explicitly to the my_function defined within the my_code namespace. From within the my_code namespace we do not need to use the scope resolution operator to refer to my_code::my_function, we can implicitly refer to it as my_function. However, if we need to explicitly refer to the global my_function then we must use ::my_function. Polluting the global namespace is never a good idea. If we include code from various libraries that do not use namespaces, then we could easily end up with multiple definitions for the same name. Thus all libraries should place their code within a unique namespace. Namespaces can also be imported into the global namespace and thus minimise the need for scope resolution, simplifying your code. While this is handy for trivial code, we should avoid doing so in large, complex code. Only import what actually needs to be imported and localise those imports to the specific translation units or the specific code that actually requires them. Never import namespaces in a header file because headers are intended to be included wherever they are needed and this can easily result in hidden mass pollution of the global namespace.

What are some disadvantages of using namespace in C?

Here's one: there's no namespace in C

What is global object in c plus plus?

A global object is any object instantiated in the global namespace. The global namespace is anonymous, so if we don't explicitly specify a namespace prior to instantiating an object, that object will be instantiated in the global namespace: int x; // global namespace n { int x; // non-global }; To refer to the non-global, we must use namespace resolution: x = 42; // assign to the global n::x = 42; // assign to the non-global

Can you have nested namespace?

Of course! All namespaces are nested by default since all namespaces exist in the global namespace. A class is also a namespace; therefore classes can also be nested.

Is a public function accessed like a non-member function?

A public function is scoped to the class in which it is declared. If declared non-static, then it must be invoked against an instance of the class but if declared static then namespace resolution is required to access the function. A non-member function is not scoped to any class but may be scoped to a namespace. If no namespace is specified, then it is scoped to the (unnamed) global namespace. If scoped to any other namespace then namespace resolution is required to access the function.

Why is namespace std used in c?

Namespaces in general help keep code organised and ultimately avoid polluting the global namespace. That is, multiple namespaces can use the same names for different purposes without causing name-clashes. A class, struct or union is also a namespace. The std namespace contains all standard library names, including standard template library names. The namespace is also subdivided to separate different features of the library. Although namespaces can make code less readable, you can import names into the global namespace to help simplify code without polluting the global namespace. This is achieved by importing names locally, within those functions and classes that specifically require those names. In trivial programs, it is common practice to import all standard library names into the global namespace but in real-world programs imports are highly localised.

Difference between domain DFS and stand alone DFS in windows 2k?

Stand-alone DFS Namespace In a stand-alone DFS namespace, the path to access the root or a link starts with the root server name. The stand-alone DFS root can comprise of a single root target. Therefore, these are not fault tolerant. When the root target is not available, you cannot access the complete DFS namespace. You can enable fault tolerance on a stand-alone DFS namespace by creating these namespaces on a cluster of servers. A stand-alone DFS namespace has its configuration information stored in the local registry of eth root server. Domain-based DFS Namespace In a domain-based DFS namespace, the path to access the root or a link starts with the domain name of the host. The domain-based DFS root can comprise of single or multiple root targets that enables fault tolerance and load sharing. A domain-based DFS namespace has its configuration information stored in the Active Directory. Exemple : • \\DomainName\RootName: This is the format of the namespace when you select the Domain-based DFS namespace type. • \\ServerName\RootName: This is the format of the namespace when you select the Stand-alone DFS namespace type.

Is using namespace STD compulsary for functions in c plus plus syntax?

No, the use of 'namespace std' is not compulsory. You can specifiy it on any object reference. Specifying 'namespace' simply provides a default value. Contrast ... using namespace std; cout << "Hello world!" << endl; ... with ... std::cout << "Hello world!" << std::endl;

What is scope resolution operator and how it can be used for global variable?

Wahen we say scope we're referring to the enclosing context of a member. In C++, the scope or context of a member is defined by its enclosing namespace. A namespace allows us to completely separate all the enclosed members from all the members of all other namespaces. Namespaces can also enclose other namespaces. Members that do not have an enclosing namespace of their own are said to exist within the global namespace -- effectively a namespace with no name. However, the global namespace also provides the enclosing context for all other namespaces. That is, namespaces create a hierarchy or "family-tree" where the global namespace serves as the root. Note that although namespaces are typically created by using the namespace keyword, we also create namespaces whenever we declare a class, struct, union or enum. That is, a class name is a namespace in its own right. If that class is not defined within the context of any other namespace then it implicitly exists within the global namespace. Namespaces literally allow us to separate names (variable names, function names and class names, etc) into separate "spaces". That is, two namespaces can share the same name provided they exist within separate namespaces. However, it is often necessary for the members of one namespace to refer to the members of another namespace. This is achieved by using the scope resolution operator. If we do not use scope resolution, the compiler will search for the name within the current namespace and if no such name exists, it will search the global namespace. If the name cannot be found in either, a coimpiler error occurs. With regards to global variables, we do not need to use scope resolution unless the global variable has a name that also exists within the current namespace. But since the global namespace has no name, we simply omit the namespace that would normally preceed the scope resolution operator. For instance, if the global variable were named matrix and the current namespace also happened to contain the same name, we can refer to the global instance as ::matrix. Of course we could easily avoid such problems by choosing more appropriate names. Variables in the global namespace should always be given the prefix "g_", thus our global matrix becomes g_matrix. By the same token, member variables should be given the prefix "m_", thus our namespace's matrix becomes m_matrix. While this resolves any potential name-clashes or ambiguity with regards member data and global data, prefixing global functions and member functions in this way would be considered quite unsatisfactory. In these cases scope resolution is the ideal solution. Of course, it would be better to avoid global data altogether, but that's a different topic entirely.

What is the purpose of the scope resolution operator in c plus plus?

The scope resolution operator (::) applies to namespaces. Namespaces help us to organise our code, avoid name clashes with existing code, and to minimise pollution of the global namespace. Namespaces can be nested, thus allowing us to modularise our code into logical units. A class definition is also a namespace. We use scope resolution to refer to names in different namespaces. Names within the current namespace do not require scope resolution, in the same way that a class can refer to its immediate members without the need for scope resolution. We also need scope resolution when a nested namespace masks a name in an enclosing namespace. Note that the global namespace encloses all namespaces declared with global scope: void f (); // global name (function) namespace X { int f (); }; int main () { int f; // local name (masks the global name) f (); // error -- cannot call an int ::f(); // ok -- invoke global function f X::f(); // ok - invoke the f in namespace X // ... } Note that the global namespace has no name, hence all names are defined in the global namespace by default unless we introduce a (named) namespace. If we use the scope resolution operator without a namespace prefix, we are implicitly referring to a global name. If we omit the scope resolution operator entirely, then we are referring to a local name ( a name within the current namespace or function). Namespace X is also in the global namespace, thus we could refer to ::X::f() rather than X::f(). However, because X is visible in the global scope (and is not masked by a local name), there's no need to overly verbose; we only qualify as much as needs to be qualified with scope resolution. Unlike classes, namespaces remain open. This allows us to add new names to an existing namespace: namespace X { int g (); }; Here, the X namespace now has two members: X::f() and X::g(). The std namespace is define by the C++ standard library. Like any other namespace, we can re-open it to add new names to it. However, just as we should avoid polluting the global namespace, we should also avoid polluting existing namespaces other than our own, particularly those defined by the standard library. If we really wish to extend the standard library with bespoke functionality, use another namespace such as "estd". This helps to keep your bespoke code completely separate from the standard library.

What is in java?

This imports the classes in the java.swing package. This means that you can use the classes without having to type out their whole namespace.

What is naming space in c plus plus?

A namespace is a group of related identifiers.namespace ns {int i;double d;}Inside namespace ns, i and d can be used normally. Outside namespace ns, i is called ns::i and d is called ns::d. To import i into the current scope, say "using ns::i;". To import all identifiers in ns into the current scope, say "using namespace ns;". Namespaces can be nested:namespace ns1 {namespace ns2 {int i;}int i;}The i in namespace ns1 is fully qualified as ns1::i. The i in namespace ns2 is fully qualified as ns1::ns2::i. The two variables are distinct. Inside ns2, i refers to ns1::ns2::i; inside ns1, i refers to ns1::i.

What is the name of the namespace that contains the primary data types and core classes such as the Console class?

The context seemed to be in C# or VB.Net. System is the namespace.For Java, the package (not namespace) is java.lang

How do you resolve name-space collision?

Import only one of the packages containing the classes with the same name. Use the other class by typing out its full namespace.

What is the Difference between package and namespace?

kai ni bhai....

What is namespace collision in java?

ggagagggagag gghakk

What DNS servers are at the highest level of the DNS namespace?


What is namespace in programming?

A namespace is a logical grouping of statements in some object-oriented programming languages, such as C++ and Java (No, not C. C is not object-oriented). Inside of namespaces, you can have classes, functions, variables, etc.

Initialization of variables in namespace in C plus plus?

Is an important thing to do.

Why do optimisation questions exist?

The type or namespace name 'Optimization' does not exist.

What is the namespaces in Net framework?

They are(simply put) the things that you import.... EXAMPLE: VB Import (namespace) C# Using (namespace)