Using "using" directive to shorten function definition
I have stumbled upon an unusual to me usage of using namespace directive:
Given a header file WeirdNamespace.h:
namespace WeirdNamespace
{
class WeirdClass
{
public:
int x;
void go();
};
}
I have a matching 'WeirdNamespace.cpp`:
#include "WeirdNamespace.h"
#include <iostream>
using namespace WeirdNamespace;
void WeirdClass::go()
{
std::cout << "Reached go?!" << std::endl;
}
The class is used as follows:
#include "WeirdNamespace.h"
int main(int argc, const char * argv[])
{
WeirdNamespace::WeirdClass c;
c.go();
}
Until now I have never seen the using directive used to avoid prefixing
method names with the namespace name. Is it a correct usage of the
directive? Are there any pitfalls specific to this scenario, except for
the usual using namespace caveats?
No comments:
Post a Comment