Solved

about namespaces

Posted on 2000-03-01
5
251 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 50 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

734 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