"Using"仅在当前类上具有作用域的声明?
"Using" declaration with scope only on current class?
是否有可能有一个范围仅限于单个类的using
指令?
请注意,我想"使用"的内容不包含在当前类的父类中。
为简单起见,假设以下示例如下:
#include<vector>
class foo
{
using std::vector; //Error, a class-qualified name is required
}
另一个有趣的事情是,如果包含using
指令,如果包含标头:
MyClassFoo.h:
#include<vector>
using std::vector; //OK
class foo
{
}
而在
NewHeader.h
#include "MyClassFoo.h"
...
如何防止"using std::vector
"在这里可见?
既然你标记了 c++11:
#include<vector>
class foo
{
template<typename T>
using vector = std::vector<T>;
};
至于你的第一个要求,你可以使用命名空间,以便using namespace
的范围限制为单个类。
#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
对于第二种情况,请明智地利用#define
和#undef
。
相关文章:
- 未在作用域中声明unordered_map
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 未在此作用域中声明的函数和变量 (C++)
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 重载时未在 C++ 的作用域错误中声明
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- Tictactoe 游戏变量未在作用域中声明
- 当变量在多个函数作用域中使用时,我应该在类 private 中声明该变量吗?
- 如何在C++中嵌套词法作用域可访问的作用域中声明静态信息?
- gets()未在作用域中声明
- 在C++14中,在哪个作用域中声明了重新声明枚举的非范围枚举器
- C++11 外部作用域变量声明为 auto
- 未在此作用域中声明类 (c++)
- 如何声明在函数作用域中定义的结构
- 未在此作用域中声明函数,即使存在头文件也是如此
- 变量声明中结构的作用域解析是什么意思
- 在C++中,在给定的相同作用域内声明相同的变量名
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 类未在作用域中声明,但已声明该类
- 使用新的外部函数作用域声明和初始化变量