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

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

本文介绍了与硬件接口的 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++ 构造函数应该真正起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:与硬件接口的 C++ 构造函数应该真正起作用吗?

基础教程推荐