dive into the world of C++

Hello friends
As we know that the current verson of c++ introduces the keyword(NAMESPACE) . But still many of us don't exactly know the use of it.While going through one book i noticed the use of it and wanted to share with all my friends.

namespace is a scope. Namespaces are used to make logical groupings and to avoid potential naming conflicts within C++ applications. Use of namespaces incurs no overhead to application size or performance. To use the contents of a namespace, its contents or those parts of it that are required must be brought into current scope which can be achieved in a number of different ways:

eg. To bring an entire namespace into current scope, use the 'using' keyword, eg:

using namespace std;

This brings the whole of the standard template library (STL) into current scope. This is generally only used in examples; bringing the entire contents of such a namespace into current scope in this fashion defeats the purpose of namespaces. To bring a single namespace member into scope, the using keyword can be used to explicitly refer to that item, eg:

using std::string;

This brings the STL 'string' type into current scope. Alternatively you can simply declare your variable with full namespace syntax within code. eg:

std::string my_string;

This declares my_string as an object of the STL string type.

Using Namespaces In Your Own Applications
Since a namespace is a scope, they can be used to aggregate code with similar intent or functionality. For example, you may have a library of useful utility functions and/or classes. Grouping them together within a subjectively logical namespace and calling it 'utils' would be accomplished thus:

Replies

  • rishi0922
    rishi0922
    Can you please elaborate "logical grouping" and "potential naming conflicts" ...
  • xxxabhash007
    xxxabhash007
    Logical Grouping is the grouping of the objects having dependencies and interactions among themselves and grouping them into an activity to show behaviour.
  • xxxabhash007
    xxxabhash007
    Potential naming conflicts is the simple conflicts of variable names which we use in declaration part of the program.
  • xxxabhash007
    xxxabhash007
    I will tell you one thing Anand, using namespaces will solve conflicts for C++ but this will not solve potential conflict for C services and clients.

You are reading an archived discussion.

Related Posts

Hello CEans, Cooltwins aka Madhumithais one of the most active CEans on CrazyEngineers. Hailing from Chennai she helps everyone at CE and comes up with interesting discussions. We took out...
Hi there friends !! anybody who wants to - create new ideas new inventions or to try new things create new prototypes and interested to become a problem solver or...
Hello friends, Greetings from the Shaastra Organizing Team, IIT Madras! It's our pleasure to introduce to you Shaastra Circuit Design Challenge (SCDC),to be held as part of Shaastra 2010,the ISO...
see the power of 1kb of javascript https://js1k.com/demosmy blog:https://novicetoprogramming.blogspot.com/
Hi CEs, I am pursing my degree from National university of Singapore with Microelectronics Specialization 😀 Hoping to get crazy friends with extreme engineering ideas 😀\ cheers, Prakash Neo