温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++怎么使用指向实现的指针技术获得稳定的ABI

发布时间:2021-11-26 13:59:43 来源:亿速云 阅读:142 作者:iii 栏目:大数据

本篇内容介绍了“C++怎么使用指向实现的指针技术获得稳定的ABI”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

I.27: For stable library ABI, consider the Pimpl idiom(考虑使用指向实现的指针技术获得稳定的ABI)

Reason(原因)

私有数据成员参与(内存上的)类布局,私有成员函数参加重载判断,因此改变这些实现细节需要重新编译类的所有用户。一个非多态的接口类管理一个指向实现的指针(Pimpl)可以通过一次间接访问的代价消除修改类实现对类用户的影响。

译者注:形式上很像桥接模式,但目的是为了隐藏而不是多态。

Example(示例)

interface (widget.h)

class widget {    class impl;    std::unique_ptr<impl> pimpl;public:    void draw(); // public API that will be forwarded to the implementation    widget(int); // defined in the implementation file    ~widget();   // defined in the implementation file, where impl is a complete type    widget(widget&&) = default;    widget(const widget&) = delete;    widget& operator=(widget&&); // defined in the implementation file    widget& operator=(const widget&) = delete;};

implementation (widget.cpp)

class widget::impl {    int n; // private datapublic:    void draw(const widget& w) { /* ... */ }    impl(int n) : n(n) {}};void widget::draw() { pimpl->draw(*this); }widget::widget(int n) : pimpl{std::make_unique<impl>(n)} {}widget::~widget() = default;widget& widget::operator=(widget&&) = default;
    Notes(注意)

See GOTW #100 and cppreference for the trade-offs and additional implementation details associated with this idiom.

关于这个用法平衡考虑和另外的实现细节可以参考GOTW #100和cpprefernce网站。

Enforcement(实施建议)

(Not enforceable) It is difficult to reliably identify where an interface forms part of an ABI.

(无法强制)很难可靠地识别哪里的接口定义会成为ABI的一部分。

译者注:也就无法决定是否需要提示用户。

“C++怎么使用指向实现的指针技术获得稳定的ABI”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI