Reputation: 945
Is there a possibility to have a using
directive whose scope is limited to a single class?
Note, that what I want to "use" is not contained in the parent of the current class.
For simplicity, assume the following exmple:
#include<vector>
class foo
{
using std::vector; //Error, a class-qualified name is required
}
Another interesting thing to know is, if the using
directives are included, if a header is included:
MyClassFoo.h:
#include<vector>
using std::vector; //OK
class foo
{
}
And in
NewHeader.h
#include "MyClassFoo.h"
...
how can I prevent "using std::vector
" to be visible here?
Upvotes: 8
Views: 3186
Reputation: 13288
Since you tagged c++11:
#include<vector>
class foo
{
template<typename T>
using vector = std::vector<T>;
};
Upvotes: 4
Reputation: 1394
As for your first requirement, you can use a namespace so that the scope of using namespace
is limited to a single class.
#include<vector>
namespace FooClasses
{
using namespace std; //The scope of this statement will NOT go outside this namespace.
class foo
{
vector<int> vecIntVector;
};
}// namespace FooClasses
For your second case, do make use of #define
and #undef
wisely.
Upvotes: 0