In g(), to what does the call, f(‘a’) resolve? How could the code be modified so that the call still resolves to A::f(int)?

Suppose we have the following namespace:
```
namespace A
{
void f(int);
//. . .
}
using namespace A;

// In the global namespace
void g()
{
f(‘a’); // calls A::f(int)
}
```

In the global namespace, the function g( ) calls f(‘a’). There is no problem here, f(‘a’) is unambiguously A::f(int). Now suppose at some later time, the following namespace grouping and using directive is inserted prior to the call to function f.
```
namespace B
{
void f(char);
//. . .
}
using namespace B;
```

For convenience, all these are listed again.
namespace A
```
{
void f(int);
//. . .
}
using namespace A;

namespace B
{
void f(char);
//. . .
}
using namespace B;
// In the global namespace
void g()
{
f(‘a’);
}
```

The call, f(‘a’) resolves to B::f. The using namespace directives make both A::f(int) and B::f(char) are available, and so the resolution is made by exact match.
Modified code that cause A::f(int) to be called in g() follows:
```
#include
using std::cout;
namespace A
{
void f(int);
//. . .
}
void A::f(int) { cout << "A::f(int)\n"; }

using namespace A;
//using A::f;
namespace B
{
void f(char);
//. . .
}
void B::f(char) { cout << "B::f(char)\n"; }
using namespace B;
//using B::f; // We could write this instead
// In the global namespace
void g()
{
cout << "::g() \n";
A::f('a'); // forces f(int)
B::f('a'); // forces f(char)
}
int main()
{
g();
}
```

Computer Science & Information Technology

You might also like to view...

________ software exists because the basic needs of the business are still met by the software

Fill in the blank(s) with correct word

Computer Science & Information Technology

Which of the following is the correct recursive step for the recursive function power (m, n) which raises m to the power n?

a) if (n <= 1) return m; b) return m * power (m - 1, n); c) return n * power (m, n - 1); d) return power (m, n-1);

Computer Science & Information Technology