简体   繁体   English

从 POD 结构继承的类中默认构造函数的奇怪行为

[英]Strange behavior of default constructor in a class inherited from POD struct

This question relates to this one .这个问题和这个有关

As I mentioned in previous question I've decided to inherit my class from Win structure BITMAP to provide some extended functionality.正如我在上一个问题中提到的,我决定从 Win 结构 BITMAP 继承我的类以提供一些扩展功能。

I've noticed interest detail in compiled program behavior.我注意到编译程序行为中的兴趣细节。 First I have defined default constructor for my class like below:首先,我为我的类定义了默认构造函数,如下所示:

CPreviewFrame::CPreviewFrame():
   m_bufferSize( 0 )
{
   bmBits = NULL; //ensure that compiler in debug won't init it with 0xccccc... and delete[] will do the job
}

In idea compiler had to generate code which calls default constructor for base type even if it wasn't called manually in init list.在idea中,编译器必须生成为基类型调用默认构造函数的代码,即使它没有在init列表中手动调用。 But while debugging I noticed that BITMAP's data members are not initialized!但是在调试时我注意到 BITMAP 的数据成员没有初始化! I added manual initialize for BITMAP and it worked - all data members were initialized by zeros:我为 BITMAP 添加了手动初始化并且它起作用了 - 所有数据成员都由零初始化:

CPreviewFrame::CPreviewFrame():
   BITMAP( ),
   m_bufferSize( 0 )
{
   //bmBits = NULL; //it's not needed anymore probably
}

Why does it happen?为什么会发生? Isn't compiler obligated to call default constructor or it's applied to classes only?编译器不是有义务调用默认构造函数还是仅应用于类? (it can't be so I think - only difference is in default access qualifiers for members and for inheritance) (我认为不可能如此 - 唯一的区别在于成员和继承的默认访问限定符)

If you do not provide an explicit initializer for a POD-struct, then the object has an indeterminate initial value per Section 8.5/9 of the C++ standard.如果您没有为 POD 结构提供显式初始值设定项,则根据 C++ 标准的第 8.5/9 节,该对象具有不确定的初始值。 Adding an initializer for BITMAP that is an empty set of parenthesis to the initializer list of your CPreviewFrame constructor value-initializes the BITMAP object per Section 8.5/7.添加一个初始BITMAP是一组空括号到您的初始化列表CPreviewFrame构造函数值初始化BITMAP每科8.5 / 7对象。 According to Section 8.5/5, that will mean all the non-static members of BITMAP will be zero-initialized since they are not arrays, unions, or class-types.根据第 8.5/5 节,这意味着BITMAP所有非静态成员都将被零初始化,因为它们不是数组、联合或类类型。

In your initial example though, you only initialized the bmBits member of the BITMAP structure in the actual body of the CPreviewFrame constructor ... that leaves the rest of the data-members of BITMAP with values that are indeterminate since no initializer was specified for the BITMAP structure itself.但是,在您最初的示例中,您仅在CPreviewFrame构造函数的实际主体中初始化了BITMAP结构的bmBits成员......这使BITMAP的其余数据成员具有不确定的值,因为没有为BITMAP结构本身。 Since each non-static data-member of a class is initialized before the actual body of the constructor is called, the lack of an explicit initializer for BITMAP , which is a non-static POD-struct data-member of your CPreviewFrame class, means that the behavior described in 8.5/9, where the values are set to an indeterminate initial value, takes effect.由于类的每个非静态数据成员都在调用构造函数的实际主体之前被初始化,因此BITMAP缺少显式初始化程序,它是CPreviewFrame类的非静态 POD 结构数据成员,意味着8.5/9 中描述的行为,其中值被设置为不确定的初始值,生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM