Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

about namespaces

Posted on 2000-03-01
5
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 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