answersLogoWhite

0

What is the use of namespace?

Updated: 8/10/2023
User Avatar

Wiki User

9y ago

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

17y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the use of namespace?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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


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

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 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


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 are some disadvantages of using namespace in C?

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

Related questions

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


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

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 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


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

System.Runtime


What is Namespace in NET framework?

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


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 are some disadvantages of using namespace in C?

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


What is the user folder for an account that contains a group of subfolders called?

profile namespace


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.


What is the namespaces in Net framework?

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