加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 关于RAII,STL pop和PIMPL的基本问题

发布时间:2020-12-16 06:03:27 所属栏目:百科 来源:网络整理
导读:在今天阅读proggit时,我在 post发表了关于Google Ai挑战中的顶尖地位是由C采取的评论.用户reventlov声明 The biggest problem I have with C++ is that it’s waaay too easy to think that you’re a “C++ programmer” without really understanding all
在今天阅读proggit时,我在 post发表了关于Google Ai挑战中的顶尖地位是由C采取的评论.用户reventlov声明

The biggest problem I have with C++ is that it’s waaay too easy to think that you’re a “C++ programmer” without really understanding all the things you need to understand to use C++ acceptably well.

You’ve got to know RAII,and know to use namespaces,and understand proper exception handling (for example,you should be able to explain why the pop() methods in the STL do not return the values they remove). You’ve got to know which of the three generations of functions in the standard library is the right one. You should be familiar with concepts like PIMPL. You need to understand how the design of the standard library (especially the STL) works. You need to understand how macros interact with namespaces,and why you usually shouldn’t use macros in C++,and what you should use instead (usually templates or inlines,rarely a class). You need to know about boost.

我想我是他提到的那些无耻的C程序员之一.为了保持这个简短,我的问题是

>你能给出一个典型的RAII监管错误的例子,例如哪里的最佳实践决定使用RAII,但程序员已经采用其他方法?
>为什么STL中的pop()方法不返回它们删除的值?
>我读了PIMPL的维基百科条目,不了解它.你可以举一个典型的PIMPL成语用法的例子.

解决方法

> RAII是关键但有时被遗忘的一个很好的例子是锁定互斥体时.如果您有一段代码锁定互斥体,执行操作,然后解锁它,如果操作引发异常或以其他方式导致线程死机,互斥体将保持锁定状态.这就是为什么有几个范围锁定类(如 QMutexLocker),因为如所述的 here,您可以保证析构函数运行.所以如果你使用范围锁定,它将永远解锁,防止死锁.
> Pop为了速度而返回void: SGI FAQ,并防止对象复制构造函数抛出的异常.
> PIMPL被Qt框架大量使用以提供二进制兼容性.它允许您从公共API隐藏数据结构的所有内部.这意味着,如果要将私有成员添加到类中,则将其添加到d指针.这维护着 Binary Code Compatibility,因为唯一的暴露的数据成员是一个指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读