typedef 或在我的命名空间中公开外部库类时使用的 using 关键字
typedef or using keyword when exposing external library class in my namespace
我想向我的库用户公开外部库中的类。 具体来说,我想将类"导入"到我的命名空间,这样用户就不需要知道我在幕后使用了哪些库。 通常,我似乎可以通过使用typedef
来做到这一点,或者简单地using
类。 有什么理由选择一种方法而不是另一种方法(或做其他事情)吗? (我的教育:)似乎有一些差距)
例如:我想创建一个使用 Boost::Asio 的串行端口管理器。
namespace MySerialManager {
//should I use a typedef
typedef boost::asio::serial_port_base::flow_control flow_control ;
//or a using...
using boost::asio::serial_port_base::flow_control;
class SerialManager
{
//let the user specifify the flow on construction
SerialManager(const flow_control& fc);
}
}
或者我应该完全做别的事情...... 非常感谢。
两者具有相同的最终结果。 使用可能更接近您的意图。
如果你想"导入"一个类模板,那么using
将是你唯一的选择。就目前而言,我认为没有任何实质性差异。就个人而言,在上述情况下,我会选择typedef,因为它是一个更古老,更熟悉的结构。
相关文章:
- 是否建议在函数中使用using关键字
- 如何使用 "using" 关键字定义函数原型/签名
- C++使用 using 关键字键入别名
- C++带有 using 关键字和参数包的模板函数
- 在 C++ 中,在以下情况下"using"如何使用关键字?
- 如何将"using"关键字用于可变参数模板
- 如何在C++中使用"using"关键字
- 使用 "using" 关键字继承基类的复制和移动构造函数
- 在纯虚函数上应用 "using" 关键字C++
- C++- "using"关键字或使用带有范围解析运算符的命名空间名称
- 使用 "using" 关键字作为伪预处理器
- 如何在方法中使用 make "using" 关键字以接受它作为类级别
- "using"关键字在 c++ 中究竟有什么作用?
- 在 C++ 中使用关键字 using 声明模板化函数指针的语法
- 智能指针删除器和"using"名称为"pointer"关键字
- C++中"using"关键字背后的逻辑是什么?
- 撤消按关键字隐藏的名称"using"。在孙子班不工作
- C++11`using`关键字:专门化模板参数的模板别名
- "using" C++标头实现中的关键字
- C++中的关键字"using"可以与"namespace"以外的内容一起使用吗?