初始化非指针类成员
最近,我从广受好评的 中阅读了很多有关构造函数的内容C++ 常见问题解答。其中一个条目提到,最好始终使用初始化列表,而不是在构造函数本身的代码块中初始化类成员。
这是因为编译器倾向于创建类成员的多个副本,而不是简单的一份副本。
示例
好
Foo::Foo( void )
: mBar( new Bar ) //pointer to some memory address
{
}
坏
Foo::Foo( void )
{
mBar = new Bar;
}
它还说明了一件事(虽然这与构造函数有关,但它也与一般对象的纯粹初始化有关,甚至从非成员函数)是指通过以下方法初始化对象时:
void f( void )
{
Foo foo( Bar() ); //Bad.
foo.x(); //error
}
您会,我引用:“[声明]返回 Foo 对象的非成员函数”
。
(有关更多信息,请单击上面的链接)
问题
因此,以下情况是否不明智:
Geometry::Geometry( void )
: mFaces( QVector< GLushort >() ),
mFinalized( false ),
mNormals( QVector< QVector3D >() ),
mVerticies( QVector< QVector3D >() )
甚至是:
Geometry::Geometry( void )
: mFaces( QVector< GLushort > ),
mFinalized( false ),
mNormals( QVector< QVector3D > ),
mVerticies( QVector< QVector3D > )
由于这些分配的方式(即,这些分配的方式)是非指针),这让我想知道这些对象是否在一开始就需要初始化。如果他们这样做,这是正确的方法吗?或者说有没有更好的初始化方法?
这与问题有何关系
这与一般问题相关,因为 C++ 构造函数初始化背后的方法与使用构造函数初始化对象有关,而且我不知道是否不是在堆栈上分配的对象 - 或者,所以我相信 - (无论哪种方式,没有指针分配的对象)甚至首先需要初始化。
Lately, I've been reading much about constructors from the well-received C++ FAQ. One of entries mentions that it's always best to use initialization lists, as opposed to initializing class members within the code-block of the constructor itself.
This is because the compiler tends to create multiple copies of the class members, rather than simply one copy.
Example
Good
Foo::Foo( void )
: mBar( new Bar ) //pointer to some memory address
{
}
Bad
Foo::Foo( void )
{
mBar = new Bar;
}
One thing it also states (and, while this relates to constructors, it also relates to pure initialization of objects in general from even non-member functions ) is that when initializing an object through methods such as the following:
void f( void )
{
Foo foo( Bar() ); //Bad.
foo.x(); //error
}
You will, and I quote: "[declare] a non-member function that returns a Foo object"
.
(for more info, click the link above)
The Question
Because of this, is it unwise to have the following:
Geometry::Geometry( void )
: mFaces( QVector< GLushort >() ),
mFinalized( false ),
mNormals( QVector< QVector3D >() ),
mVerticies( QVector< QVector3D >() )
Or even this:
Geometry::Geometry( void )
: mFaces( QVector< GLushort > ),
mFinalized( false ),
mNormals( QVector< QVector3D > ),
mVerticies( QVector< QVector3D > )
Because of the way that these are allocated (i.e., the fact that these are non-pointers), it makes me wonder whether or not these objects even NEED initialization in the beginning. If they do, is this the correct method of going about it? Or, is there a better method of initialization?
How this relates to the question
This relates to the general question due to the way that the methodology behind C++ constructor initialization relates to both initializing objects using constructor functions, along with the fact that I am ignorant to whether or not objects allocated on the stack - or, so I believe anyway - (either way, objects without pointer allocation) even need initialization in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果成员变量属于具有用户声明的默认构造函数的类类型,则无需在初始化列表中显式提及它:无论如何,在构造期间、输入构造函数主体之前,都会调用其默认构造函数。
如果成员变量是原始类型(例如
int
或bool
),或者是没有任何用户声明的构造函数的类类型,则需要对其进行初始化明确地,否则它将不会被初始化(并且它将具有未知的值;您无法读取未初始化的对象)。If a member variable is of a class type with a user-declared default constructor, you do not need to explicitly mention it in the initialization list: its default constructor will be called anyway, during construction, before the body of the constructor is entered.
If a member variable is a primitive type (like
int
orbool
) or is of a class type that doesn't have any user-declared constructors, you need to initialize it explicitly, otherwise it will not be initialized (and it will have an unknown value; you cannot read an uninitialized object).