Assume I have the method defined in the two different namespaces:
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
//do work...
}
}
The foo::print()
and the bar::print()
functions are absolutely equal. My project uses the numerous calls of these functions.
Is there a way to remove one of the print()
definitions without changing the calls of these function? I mean something like the following (of course, C++ language doesn't allow this construction, it's just an example):
namespace foo, bar //wrong code!
{
void print()
{
//do work...
}
}
If there is no way to refactor the code as I want, please tell me, do you like the following decision? Will you be glad if your project contains such code? :)
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
foo::print();
}
}
ADD:
Thank you guys, I'm fully satisfied by your answers. Just one moment I want you to clarify: is there a difference between using ::foo::print
and using foo::print
?
Probably the best solution is this:
However you can write:
This is not recomended, but there is no evil. Compilers are smart enough to understand that these two functions are the same and can be used the same assembly code.
Remember,
namespace
keyword is for programmers, compiler doesn't care about them very much, for compiler namespaces are like prefix for functions/methods/classes.With a
using
declaration:Sample
You can achieve this with a
using
declaration.EDIT
Regarding the difference between
::foo::print
andfoo::print
: prepending a qualified name with :: means that you explicitly refer to the one in the global namespace. This can be used to select the global one, even if there is another item with the same name closer in scope.How about
using
declaration:Using
::foo::print
instead offoo::print
is an important point. If you would have anotherfoo
inside ofbar
:then you'd see the merit of using
::
. To summarize, prepending::
is a safe way to ensure that another nested namespacefoo
, which could be potentially added in future, will not bring you any surprises.