• <tfoot id='k4irA'></tfoot>

        <i id='k4irA'><tr id='k4irA'><dt id='k4irA'><q id='k4irA'><span id='k4irA'><b id='k4irA'><form id='k4irA'><ins id='k4irA'></ins><ul id='k4irA'></ul><sub id='k4irA'></sub></form><legend id='k4irA'></legend><bdo id='k4irA'><pre id='k4irA'><center id='k4irA'></center></pre></bdo></b><th id='k4irA'></th></span></q></dt></tr></i><div id='k4irA'><tfoot id='k4irA'></tfoot><dl id='k4irA'><fieldset id='k4irA'></fieldset></dl></div>
          <bdo id='k4irA'></bdo><ul id='k4irA'></ul>
        <legend id='k4irA'><style id='k4irA'><dir id='k4irA'><q id='k4irA'></q></dir></style></legend>

      1. <small id='k4irA'></small><noframes id='k4irA'>

        我应该避免哪些 C++ 陷阱?

        时间:2024-05-11
          <bdo id='MjU7X'></bdo><ul id='MjU7X'></ul>
          <tfoot id='MjU7X'></tfoot>
        • <legend id='MjU7X'><style id='MjU7X'><dir id='MjU7X'><q id='MjU7X'></q></dir></style></legend>

        • <i id='MjU7X'><tr id='MjU7X'><dt id='MjU7X'><q id='MjU7X'><span id='MjU7X'><b id='MjU7X'><form id='MjU7X'><ins id='MjU7X'></ins><ul id='MjU7X'></ul><sub id='MjU7X'></sub></form><legend id='MjU7X'></legend><bdo id='MjU7X'><pre id='MjU7X'><center id='MjU7X'></center></pre></bdo></b><th id='MjU7X'></th></span></q></dt></tr></i><div id='MjU7X'><tfoot id='MjU7X'></tfoot><dl id='MjU7X'><fieldset id='MjU7X'></fieldset></dl></div>
              <tbody id='MjU7X'></tbody>

            • <small id='MjU7X'></small><noframes id='MjU7X'>

                1. 本文介绍了我应该避免哪些 C++ 陷阱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我记得第一次学习 STL 中的向量,一段时间后,我想在我的一个项目中使用 bool 向量.在看到一些奇怪的行为并做了一些研究之后,我了解到 一个向量bools 并不是真正的 bools 向量.

                  I remember first learning about vectors in the STL and after some time, I wanted to use a vector of bools for one of my projects. After seeing some strange behavior and doing some research, I learned that a vector of bools is not really a vector of bools.

                  在 C++ 中还有其他常见的陷阱需要避免吗?

                  Are there any other common pitfalls to avoid in C++?

                  推荐答案

                  一个简短的列表可能是:

                  A short list might be:

                  • 通过使用共享指针来管理内存分配和清理来避免内存泄漏
                  • 使用资源获取即初始化 (RAII) 习惯用法来管理资源清理 - 特别是在存在异常
                  • 避免在构造函数中调用虚函数
                  • 尽可能采用极简编码技术 - 例如,仅在需要时声明变量、范围变量以及尽可能提前设计.
                  • 真正理解代码中的异常处理——包括你抛出的异常,以及你可能间接使用的类抛出的异常.这在存在模板的情况下尤为重要.
                  • Avoid memory leaks through use shared pointers to manage memory allocation and cleanup
                  • Use the Resource Acquisition Is Initialization (RAII) idiom to manage resource cleanup - especially in the presence of exceptions
                  • Avoid calling virtual functions in constructors
                  • Employ minimalist coding techniques where possible - for example, declaring variables only when needed, scoping variables, and early-out design where possible.
                  • Truly understand the exception handling in your code - both with regard to exceptions you throw, as well as ones thrown by classes you may be using indirectly. This is especially important in the presence of templates.

                  RAII、共享指针和极简编码当然不是 C++ 特有的,但它们有助于避免在使用该语言进行开发时经常出现的问题.

                  RAII, shared pointers and minimalist coding are of course not specific to C++, but they help avoid problems that do frequently crop up when developing in the language.

                  关于这个主题的一些优秀书籍是:

                  Some excellent books on this subject are:

                  • 有效的 C++ - Scott Meyers
                  • 更有效的 C++ - Scott Meyers
                  • C++ 编码标准 - Sutter &亚历山德列斯库
                  • C++ 常见问题 - Cline

                  阅读这些书对我避免了您所问的那种陷阱的帮助比其他任何事情都大.

                  Reading these books has helped me more than anything else to avoid the kind of pitfalls you are asking about.

                  这篇关于我应该避免哪些 C++ 陷阱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 std::vector<bool> 上调用 data() 会发生什么? 下一篇:如何检查元素是否在 std::set 中?

                  相关文章

                  <legend id='yQrDh'><style id='yQrDh'><dir id='yQrDh'><q id='yQrDh'></q></dir></style></legend>

                    <i id='yQrDh'><tr id='yQrDh'><dt id='yQrDh'><q id='yQrDh'><span id='yQrDh'><b id='yQrDh'><form id='yQrDh'><ins id='yQrDh'></ins><ul id='yQrDh'></ul><sub id='yQrDh'></sub></form><legend id='yQrDh'></legend><bdo id='yQrDh'><pre id='yQrDh'><center id='yQrDh'></center></pre></bdo></b><th id='yQrDh'></th></span></q></dt></tr></i><div id='yQrDh'><tfoot id='yQrDh'></tfoot><dl id='yQrDh'><fieldset id='yQrDh'></fieldset></dl></div>
                    • <bdo id='yQrDh'></bdo><ul id='yQrDh'></ul>
                    1. <small id='yQrDh'></small><noframes id='yQrDh'>

                      <tfoot id='yQrDh'></tfoot>