C++初始化列表和内存分配
以下内容有效吗?
class myClass
{
private:
...
int m_nDataLength;
boost::shared_array<int> m_pData;
...
public:
myClass(): ..., m_nDataLength(10), m_pData(new int[m_nDataLength]), ...
{
}
}
我是否正确地假设初始化将完全按照我在构造函数中给出的顺序进行?如果不是,如果 m_nDataLength 的初始化发生在 m_pData 之后怎么办?
Is the following valid?
class myClass
{
private:
...
int m_nDataLength;
boost::shared_array<int> m_pData;
...
public:
myClass(): ..., m_nDataLength(10), m_pData(new int[m_nDataLength]), ...
{
}
}
Am I right in assuming that the initialization will happen exactly in the order I've given in the ctor? If not, what if m_nDataLength's initialization happens after m_pData's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然示例中的初始化确实按照您想要的顺序进行,但这并不是您假设的原因:初始化按照类定义中数据成员声明的顺序进行。 这样做的原因是,无论使用哪个构造函数来创建对象,析构函数都必须按向后顺序销毁成员。为此,必须使用独立于构造函数的方式来定义构造顺序。
这意味着,如果不是
有人更改您的代码,
那么该代码就会出现错误。
我的建议是:
像这样的事情应该做:
While the initialization in your example does happen in the order you want, it's not for the reason you assume: Initialization happens in the order of the data members declaration in the class definition. The reason for this is that the destructor must destroy the members in backward order not matter which constructor was used to create the object. For that, a constructor-independent way of defining the construction order must be used.
That means that, if instead of
someone would change your code to
then the code would have a bug.
My advice is:
Something like this should do:
初始化将按类中的顺序初始化字段,因此:如果您进行更改
,
它将不起作用。在构造函数中,该顺序不适用。
the initialization will initialize fields by order in class, so: if you change
as
it wont work. In constructor, the order doesn't apply.
不,类成员的初始化按照成员在类定义中出现的顺序进行。如果成员出现在初始值设定项列表中,则控制用于初始化该成员的表达式(即使它使用尚未初始化的成员),但它在初始值设定项列表中出现的位置不会影响其初始化时间。
No, initialization for class members happens in the order that the members appear in the class definition. If a member appears in the initializer list, then that controls the expression used to initialize that member (even if it uses a member that has not yet been initialized) but where it appears in the initializer list does not affect when it is initialized.