C++ 何时查找初始化列表
When is an initialization list looked up by C++
我试图了解在基础 class 和 class 非静态成员实例化期间何时引用(涉及)初始化列表。我看过this article and this篇总结了一个class的初始化顺序的文章(我这里总结了一下)
Initialization shall proceed in the following order:
First, and only for the constructor of the most derived class as
described below, virtual base classes shall be initialized in the
order they appear on a depth-first left-to-right traversal of the
directed acyclic graph of base classes, where “left-to-right” is the
order of appearance of the base class names in the derived class
base-specifier-list.
Then, direct base classes shall be initialized in
declaration order as they appear in the base-specifier-list
(regardless of the order of the mem-initializers).
Then, nonstatic
data members shall be initialized in the order they were declared in
the class definition (again regardless of the order of the
mem-initializers). Finally, the body of the constructor is executed.
Finally, the body of the constructor is executed.
现在我通过阅读上面的内容了解到,首先创建基础 classes,然后创建派生 class。此外,成员按照它们在 class 定义中声明的顺序进行实例化,而不管它们在初始化列表顺序中的位置如何。
这让我觉得每次class实例化期间的基class或成员变量被实例化时(按照上面指定的顺序)C++基本上检查[=的初始化列表18=] 以查看是否为该基 class 或成员变量指定了任何特定参数或自变量。如果未指定任何内容,则 C++ 会执行默认构造函数调用。我的理解对吗。如有错误请指正
是的,你是正确的1,当你没有初始化基class或成员中的非静态成员时- initialization-list,或者当您根本不提供 member-initialization-list 时,以下内容适用:
In a non-delegating constructor, if a given potentially constructed
subobject is not designated by a mem-initializer-id (including the
case where there is no mem-initializer-list because the constructor
has no ctor-initializer), then
if the entity is a non-static data member that has a default member initializer and either
the constructor's class is a union, and no other variant member of that union is designated by a mem-initializer-id or
the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is
designated by a mem-initializer-id,
-- the entity is initialized from its default member initializer as specified in [dcl.init];
otherwise, if the entity is an anonymous union or a variant member ([class.union.anon]), no initialization is performed;
otherwise, the entity is default-initialized.
1:你说:
...If nothing has been specified then c++ does a default constructor call...
有点迂腐,上面引用的default initialized并不总是意味着将调用默认构造函数,例如,在非[=的情况下35=] 类型,如 int
,其 默认初始化 取决于存储持续时间。
我试图了解在基础 class 和 class 非静态成员实例化期间何时引用(涉及)初始化列表。我看过this article and this篇总结了一个class的初始化顺序的文章(我这里总结了一下)
Initialization shall proceed in the following order:
First, and only for the constructor of the most derived class as described below, virtual base classes shall be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base class names in the derived class base-specifier-list.
Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers). Finally, the body of the constructor is executed.
Finally, the body of the constructor is executed.
现在我通过阅读上面的内容了解到,首先创建基础 classes,然后创建派生 class。此外,成员按照它们在 class 定义中声明的顺序进行实例化,而不管它们在初始化列表顺序中的位置如何。
这让我觉得每次class实例化期间的基class或成员变量被实例化时(按照上面指定的顺序)C++基本上检查[=的初始化列表18=] 以查看是否为该基 class 或成员变量指定了任何特定参数或自变量。如果未指定任何内容,则 C++ 会执行默认构造函数调用。我的理解对吗。如有错误请指正
是的,你是正确的1,当你没有初始化基class或成员中的非静态成员时- initialization-list,或者当您根本不提供 member-initialization-list 时,以下内容适用:
In a non-delegating constructor, if a given potentially constructed subobject is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then
if the entity is a non-static data member that has a default member initializer and either
the constructor's class is a union, and no other variant member of that union is designated by a mem-initializer-id or
the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id,
-- the entity is initialized from its default member initializer as specified in [dcl.init];
otherwise, if the entity is an anonymous union or a variant member ([class.union.anon]), no initialization is performed;
otherwise, the entity is default-initialized.
1:你说:
...If nothing has been specified then c++ does a default constructor call...
有点迂腐,上面引用的default initialized并不总是意味着将调用默认构造函数,例如,在非[=的情况下35=] 类型,如 int
,其 默认初始化 取决于存储持续时间。