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.

>why and how it is possible ?

When you explicitly place a "thing" into global namespace the compiler throws an error if there's ambiguous naming, because you declared it exactly.

When you place entire namespaces in global scope, everything is included implicitly. That means that you're allowed to do what you showed in your example.

However, namespaces aside, it's usually a bad sign when you even start naming functions/objects the same name...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.