dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Jun 17, 1998

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


A Using Declaration and a Using Directive

A namespace is a scope in which declarations and definitions are grouped together. In order to refer to any of these from another scope, a full qualified name is required. However, repeating the full qualified name over and over again is tedious, error prone and less readable. Instead, a using declaration or a using directive can be used.

A using declaration is a sequence consisting of the keyword using followed by a namespace:: member. It instructs the compiler to locate every occurrence of a certain declaration (type, operator, function, etc.) in the specified namespace, as if the full qualified name were supplied:

 
#include <vector>  //STL vector; belongs to namespace std
void main() 
{
   using std::vector;  //using declaration; every occurrence of vector is looked up in std 
   vector <int> vi; //without a using declaration, a full qualified name: std::vector<int>  would be required
   //... 
}//end of main; the above using declaration goes out of scope here
A using directive instructs the compiler to recognize all members of a namespace and not just one. It consists of the following sequence: 'using namespace' followed by a namespace-name. For example:
 
#include <vector>   //STL vector; belong to namespace std
#include <iostream> //iostream classes and operators are also in namespace std
void main() 
{
  using namespace std; //directive; all <iostream> and <vector> declarations  now accessible 
   	  vector  <int> vi;
  vi.push_back(10);
  cout<<vi.begin(); 
}
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date