423,682 Members | 1,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,682 IT Pros & Developers. It's quick & easy.

What are the advantages ans disadvantages of employing the

P: 1
What are the advantages ans disadvantages of employing the "using"
directive?
Jan 13 '18 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,186
C++ requires that all names must be unique. To avoid name clashes you place names inside namespaces:

Expand|Select|Wrap|Line Numbers
  1. int x;
  2.  
  3. namespace system
  4. {
  5. struct data
  6. {
  7. int a,b,c;
  8. datat x;
  9. }
  10. namespace mystuff
  11. {
  12. double x;
  13. }
  14.  
These are three different variables with names of:

::x
system::x
mystuff::x

So far so good. Now letís say you donít like all this :: stuff in the variable name so you can tell the compiler to check a namespace for the variable name:

Expand|Select|Wrap|Line Numbers
  1. using namespace system;
  2.  
Now instead of coding system::x all you need is x because you have told the compiler you are using namespace system.


Expand|Select|Wrap|Line Numbers
  1. x= 10;
  2.  
However, this x might be ::x or system::x. If the x is ::x this code compiles. If the x is system::x, the code does not compile.

The using directive is for lazy programmers only. Top be safe you should always use fully qualified names in your
Jan 13 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.