在我的命名空间中公开外部库类时 typedef 或 using 关键字
我想将外部库中的类公开给我的库的用户。具体来说,我想将类“导入”到我的命名空间,以便用户不需要知道我在幕后使用哪些库。通常,我似乎可以通过使用 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);
}
}
或者我应该做点别的事情......非常感谢。
I want to expose a class from an external library to my library's users. Specifically I want to 'import' the class to my namespace so that the user does not need to know what libraries I am using behind the scenes. Often it seems that I can either do this by using a typedef
, or by simply using
the class. Is there any reason for choosing one method over the other (or doing something else)? (I seem to have some gaps in my education :) )
For example: I want to create a serial port manager that uses 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);
}
}
or should I be doing something else altogether.... Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者都有相同的最终结果。 using 可能更接近您的意图。
Both have the same end result. using is maybe the more close to your intent.
如果您想“导入”类模板,那么
using
将是您唯一的选择。就目前情况而言,我认为没有任何实质性差异。就我个人而言,在上述情况下我会选择 typedef,因为它是一个更古老且更熟悉的构造。If you wanted to "import" a class template, then
using
would be your only option. As it stands, I don't think there's any substantial difference. Personally, I'd go for typedef in the above case, because it's an older and more familiar construct.