在接口头文件中使用typedef是一个不好的做法吗?
Is it a bad practice to use typedef in interface header file?
我知道在头文件中使用"using namespace xxx"是一个不好的做法,但是在接口头文件中使用typedef呢?
例如:typedef std::set<unsigned int> rsids_type; //!< typedef something.
struct IRsids
{
virtual const rsids_type& GetRsids() = 0;
}
在我的选择,我认为这也是一个坏的做法,我们导入一个名称"rsids_type"到全局命名空间?那么下面的动作呢?
struct IRsids
{
typedef std::set<unsigned int> rsids_type; //!< typedef something inside the interface class.
virtual const rsids_type& GetRsids() = 0;
}
使用typedef
不必要地污染全局命名空间是语言允许的,但显然不受惯例的鼓励。
名称空间在这里(以及其他东西)是为了避免名称冲突,因此将类型定义放在名称空间和/或类中(如第二个示例)。
typedefs
通常放在头文件中,通常用作封装的一种手段(在简单的名称后面隐藏复杂的类型),例如:
typedef std::vector<std::vector<double>> Matrix
标准库实现在所有地方都这样做。
注意:
-
using namespace
应该避免在头文件中出现,因为任何头文件的包含都会带来整个命名空间。这不是typedef的情况,它基本上是一个类型别名,所以没有问题。 - 在c++ 11中,
typedef
s和using
别名是等价的:
的例子:
using Matrix = std::vector<std::vector<double>>;
相关文章:
- 挂起和取消挂起一个文件DLL
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何将class.cpp和class.hpp编译为一个.o文件
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 如何从整个磁盘中排除要装载的一个文件
- 如何将模板和非模板函数放在一个文件中
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- c++ 提升如果 .extension() == " "这意味着这个文件是一个文件夹?
- 是否可以在 c++ 中创建一个文件并为其提供属性以将其隐藏?(在 Ubuntu 上)
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?