Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

about namespaces

Posted on 2000-03-01
5
Medium Priority
?
265 Views
Last Modified: 2013-11-19
what are the namespaces in c++? what is the importance of them? can we create user defined namespaces?
0
Comment
Question by:maheshhatolkar
  • 4
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 2573791
namespaces are "scopes" in which the symbolic names you create in your source code are placed.  All names within a namespace must be unique, but names in seperate namespaces may be duplicated.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2573809
In a function, you cannot have two local variables with the same name.  However two seperate functions may have variables that have the same name, like in

void f1()
{
   int X;
   double X; // Error.
}

there is an error because the same name is used twice but in

void f1()
{
   int X;
}

void f2()
{
   double X;
}

there is no error bacuase the two names are iin seperate function, i.e. seperate scopes.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2573831
A namespace works the same way.  It is a scope in which you can define templates, classes, constants, enums and other symbolic names.  Those names must be unique within that namespace but may be used in other namespaces. like

namepace vehicles
{
    class plane
    {
    };
};

namespace geometry
{
    class plane
    {
   };
}

the two classes defined are distinct classes buth they both can have the same name because they are defined in seperate namespaces.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2573848
>> what is the importance of them?
They are used to prevent naming conflicts.  This is especially important when dealing with code libraries.  For example the STL library defines a class caled "string"  Well string is a VERY popular name.  If that name was not placed in a namespace it would conflict with the name "string" used in other circumstances.  But the name is placed in a namespace called "std"

Note to use a name that is defined in a namespace, you must preceed the name with the namespace name and a scope resolution operaotr ("::")  Like to use the STL string, you woudl specify "std::string".  Another option is to include the std::string name into the current namespace using

using std::string;

Then you don't have to specify the "std::" before "string"  Finally you could all the names defined in the std namespace into the current namespace using a single statement like

using namespace std;

>> can we create user defined namespaces?
Absolutely.
0
 

Author Comment

by:maheshhatolkar
ID: 2580335
Thanks neitod, i got really a good answer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question