应该是C ++构造函数做真正的工作吗? [英] Should a C++ constructor do real work?

查看:107
本文介绍了应该是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.

情况是我有一个表示硬件设备的类。对我来说,构造函数调用查询设备的例程以构建描述设备的实例变量是符合逻辑的。换句话说,一旦新的实例化对象,开发人员接收一个准备使用的对象,没有单独调用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有一个很好的理由为什么构造函数不应该做真正的工作?显然,它可以减慢分配时间,但是如果在分配后立即调用单独的方法,则不会有任何不同。

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在更有效的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 ++构造函数做真正的工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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