是否为成员变量自动调用默认构造函数? [英] Are default constructors called automatically for member variables?

查看:190
本文介绍了是否为成员变量自动调用默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个类:

//Awesome.h
class Awesome
{
    public:
        Awesome();
    private:
        membertype member;
}

//Awesome.cpp
#include "Awesome.h"

Awesome::Awesome()
:member()
{
}



如果省略 Awesome 的构造函数的初始化列表中的成员()将构造函数 member 自动调用?只有当我在初始化列表中不包括成员时才会调用

If I omit the member() in the initialization list of the constructor of Awesome, will the constructor of member be called automatically? And is it only called when I don't include member in the initialization list?

推荐答案


如果没有为对象指定初始值设定项,对象就是
default-initialized;如果未执行初始化,则具有
自动或动态存储持续时间的对象具有不确定的值。 [注:带有静态或线程存储持续时间的
对象是零初始化的,
参见3.6.2。 -end note]

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: objects with static or thread storage duration are zero-initialized, see 3.6.2. —end note ]

更新未来引用:默认初始化的含义定义为<默认初始化类型为T的对象意味着:` - 如果T是一个
(可能是cv),则p>

Update for future references: Further the meaning of default initialized is defined as

-qualified)类类型(第9条),调用T的默认构造函数
(如果T没有
可访问的默认构造函数,则初始化是不成立的)
;
- 如果T是一个数组类型,每个
元素是默认初始化的; - 否则,没有初始化是
。如果程序调用$ b的默认初始化$ b一个const限定类型T的对象,T是一个类型类型,具有
用户提供的默认构造函数。

To default-initialize an object of type T means:
if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

此外,它从值初始化变化,参考以下内容: -

Further it varies from value initialized referring this:-


类型T意味着:
如果T是具有用户提供的
构造函数(12.1)的
(可能是cv限定的)类类型(条款9),则T的默认构造函数(和
,如果T没有可访问的缺省
构造函数,则初始化是错误的);
- 如果T是一个(可能是cv限定的)非联合类
类型没有用户提供的构造函数,那么对象是
零初始化,如果T的隐式声明的默认构造函数
是非平凡的,那么该构造函数称为.Á - 如果T是array
type,then each element is value-initialized; - - 否则
对象为零初始化。

To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized.

这篇关于是否为成员变量自动调用默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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