与硬件接口的C ++构造函数是否应该真正起作用? [英] Should a C++ constructor that interfaces with hardware do real work?

查看:76
本文介绍了与硬件接口的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.

推荐答案

我记得在更有效的C ++中,Scott Meyers建议不要使用多余的默认构造函数。在那篇文章中,他还谈到了使用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 ++构造函数是否应该真正起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆