Hello friends,
Can anyone plz tell me the difference between a using declaration and using directive ? I know this:
A using declaration adds a particular name to the declarative region in which it occurs. whereas,
A using directive makes all the names in the namespace available without using the scope resolution operator.
Actually both seems same, since in using declaration, we don't need to use a scope resolution operator also. But see the difference as follows :
// first take using declaration
namespace Jill {
double bucket (double n) { ..........}
double fetch;
struct Hill {......}
}
char fetch;
int main()
{
using Jill:fetch //put fetch into local namespace
/* double fetch; // Error! Already have a local fetch
cin>>fetch; // read a value into Jill::fetch
cin>>::fetch; // read a value into global fetch
........
........
}
[B]//now using directive[/B]
namespace Jill {
double bucket(double n) {....}
double fetch;
struct Hill {....}
}
char fetch; //global namespace
int main()
{
using namespace Jill;
struct Hill Thrill; //create a Jill:Hill
double water= bucket(2); // use Jill::bucket();
double fetch; //not an error; hides Jill::fetch
cin>>fetch; //read a value into local fetch
cin>>::fetch; //read a value into global fetch
cin>>Jill::fetch; //read a value into Jill:fetch
.....
....
}
so u can see the difference. IN using directive we are able to declare another variable of the same name locally(means, we can use three different variables of same name), whereas in using declaration, we can use only two variables of same name, one of namespace and one of global. why and how it is possible ? can anyone plzzzz reply ?
Abhishek.