Computer Programming
Grammar
Sentence and Word Structure
C++ Programming

What is the use of a namespace?

575859

Top Answer
User Avatar
Wiki User
2006-10-13 17:00:18
2006-10-13 17:00:18

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.

1
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0

Related Questions

User Avatar

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::coutyou can make your own namespaces as wellnamespace mynamespace;void myfunction(){code for function}and use itusing mynamespace::myfunction;The main use of a namespace is to reduce or eliminate collisions with names that may be duplicated but have different functionality. For example, I may want to use an object with the name of 'cout', but that name already exists. If I place it in a different namespace I would be able to use it with that name.

User Avatar

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.

User Avatar

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

User Avatar

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;

User Avatar

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.

User Avatar

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.

User Avatar

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

User Avatar

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.

User Avatar

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

User Avatar

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.

User Avatar

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.

User Avatar

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.

User Avatar

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.

User Avatar

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

User Avatar

The simplest way to define a class, assuming you're within a namespace:class MyClassName {}And to include it in a separate namespace, and have it accessible anywhere (public):namespace MyNameSpace {public class MyClassName {}}

User Avatar

This is known as an using directive.When you write the using namespace std; directive in your program, you are informing the compiler you will be using all the names within the std namespace without qualification.So instead of using the explicit names std::cin and std::cout, you can use the implicit names cin and cout instead. The calls themselves are no different, but your source code is a little easier to read.You can also explicitly declare the names you will be using in a namespace (rather than all of them):using std::cin;using std::cout;These are known as using declarations.

User Avatar

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.

User Avatar

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.

User Avatar

You don't "have to" use "using namespace std;" in C++. It just makes typing easier. Here are two identical programs, one with, and one without.#include using namespace std;int main () {cout return 0;}#include int main () {std::cout std::endl;return 0;}

User Avatar

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.

User Avatar

A Namespace is the term used in .net to package coherent types. Namespaces is plural of that term.A namespace also provides a context shorthand, to avoid otherwise would be verbose reference.

User Avatar

If you do not include the directive using namespace stdin your program, any references to objects in namespace std will need be be qualified with that namespace. For instance...cout ... will need to be ...std::cout

User Avatar

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


Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.