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

  • <legend id='lKLhV'><style id='lKLhV'><dir id='lKLhV'><q id='lKLhV'></q></dir></style></legend>
    <tfoot id='lKLhV'></tfoot>

      1. 与硬件接口的 C++ 构造函数应该真正起作用吗?

        Should a C++ constructor that interfaces with hardware do real work?(与硬件接口的 C++ 构造函数应该真正起作用吗?)

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

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

              <tbody id='q9Ni6'></tbody>
          • <small id='q9Ni6'></small><noframes id='q9Ni6'>

                <bdo id='q9Ni6'></bdo><ul id='q9Ni6'></ul>

                  本文介绍了与硬件接口的 C++ 构造函数应该真正起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能的重复:
                  应该在构造函数中完成多少工作?

                  我一直在苦苦思索我脑海中的一些建议,但我不记得理由了.

                  I'm strugging with some advice I have in the back of my mind but for which I can't remember the reasoning.

                  我似乎记得在某个时候阅读了一些建议(不记得来源)C++ 构造函数不应该做真正的工作.相反,他们应该只初始化变量.建议继续解释真正的工作应该在某种 init() 方法中完成,在实例创建后单独调用.

                  I seem to remember at some point reading some advice (can't remember the source) that C++ constructors should not do real work. Rather, they should initialize variables only. The advice went on to explain that real work should be done in some sort of init() method, to be called separately after the instance was created.

                  情况是我有一个代表硬件设备的类.对我来说,构造函数调用查询设备的例程以构建描述设备的实例变量是合乎逻辑的.换句话说,一旦 new 实例化了对象,开发者就会收到一个可以使用的对象,不需要单独调用 object->init().

                  The situation is I have a class that represents a hardware device. It makes logical sense to me for the constructor to call the routines that query the device in order to build up the instance variables that describe the device. In other words, once new instantiates the object, the developer receives an object which is ready to be used, no separate call to object->init() required.

                  构造函数不应该做真正的工作有充分的理由吗?显然它会减慢分配时间,但如果在分配后立即调用单独的方法,那不会有任何不同.

                  Is there a good reason why constructors shouldn't do real work? Obviously it could slow allocation time, but that wouldn't be any different if calling a separate method immediately after allocation.

                  只是想弄清楚我目前没有考虑的可能导致此类建议的问题.

                  Just trying to figure out what gotchas I not currently considering that might have lead to such advice.

                  推荐答案

                  我记得 Scott Meyers 在 More Effective C++ 中建议不要使用多余的默认构造函数.在那篇文章中,他还谈到了使用 Init() 之类的方法来创建"对象.基本上,您引入了一个额外的步骤,将责任放在类的客户端上.此外,如果你想创建一个上述对象的数组,每个对象都必须手动调用 Init().你可以有一个 Init 函数,构造函数可以在内部调用它来保持代码整洁,或者如果你实现了一个 Reset(),那么对象可以调用,但是从经验来看,最好删除一个对象并重新创建它而不是尝试重置其值为默认值,除非对象被实时创建和销毁多次(例如,粒子效果).

                  I remember that Scott Meyers in More Effective C++ recommends against having a superfluous default constructor. In that article, he also touched on using methods liked Init() to 'create' the objects. Basically, you have introduced an extra step which places the responsibility on the client of the class. Also, if you want to create an array of said objects, each of them would have to manually call Init(). You can have an Init function which the constructor can call inside for keeping the code tidy, or for the object to call if you implement a Reset(), but from experiences it is better to delete an object and recreate it rather than try to reset its values to default, unless the objects is created and destroyed many times real-time (say, particle effects).

                  另外,请注意构造函数可以执行普通函数不能执行的初始化列表.

                  Also, note that constructors can perform initialization lists which normal functions could not.

                  警告不要使用构造函数进行大量资源分配的一个原因是,很难在构造函数中捕获异常.但是,有一些方法可以解决.否则,我认为构造函数的目的是做他们应该做的事情 - 为其初始执行状态准备一个对象(对于对象创建很重要的是资源分配).

                  One reasons why one may caution against using constructors to do heavy allocation of resources is because it can be hard to catch exceptions in constructors. However, there are ways around it. Otherwise, I think constructors are meant to do what they are supposed to do - prepare an object for its initial state of execution (important for object creation is resource allocation).

                  这篇关于与硬件接口的 C++ 构造函数应该真正起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                  <tfoot id='mmBDr'></tfoot>

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

                        <small id='mmBDr'></small><noframes id='mmBDr'>