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

在今天阅读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,因为唯一的暴露的数据成员是一个指针.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...