我正在学习C ++中的成员初始化列表。因此,请考虑以下示例:
struct Person
{
public:
Person(int pAge): age(pAge)
// ^^^^^^^^^ is this member initializer formally part of the constructor body?
{
}
private:
int age = 0;
};
我的第一个问题是,即成员初始化器 age(page)
正式 是构造函数的一部分。我的意思是,我已经读到,函数的身体从开口 {
开始,并在关闭}
上结束。据我当前的理解,这里涉及四件事:
- ctor定义:这包括整个
//this whole thing is ctor definition
Person(int pAge): age(pAge)
{
}
成员ritializer :这是 age(page)< /代码>部分。
-
ctor声明:这是 person(int page)
part。
-
ctor的身体:这是开口 {
和关闭}
。
之间的区域
我的第二个问题是上面给定的描述正确吗?如果不是,那么根据C ++标准,这四个术语的正确含义应是什么: em> ctor的身体。
PS:我已经阅读 this '回答我的问题。
I am learning about member initializer lists in C++. So consider the following example:
struct Person
{
public:
Person(int pAge): age(pAge)
// ^^^^^^^^^ is this member initializer formally part of the constructor body?
{
}
private:
int age = 0;
};
My first question is that is the member initializer age(pAge)
formally part of the constructor's body. I mean i've read that a function's body starts from the opening {
and end at the closing }
. To my current understanding, there are four things involved here:
- Ctor definition: This includes the whole
//this whole thing is ctor definition
Person(int pAge): age(pAge)
{
}
-
Member initializer: This is the age(pAge)
part.
-
Ctor declaration: This is the Person(int pAge)
part.
-
Ctor's body: This is the region between the opening {
and the closing }
.
My second question is that is the above given description correct? If not then what should be the correct meaning of those four terms according to the C++ standard: Ctor definition, Member initializer, Ctor declaration and Ctor's body.
PS: I've read this post which doesn't answer my question.
发布评论
评论(3)
按照
compound-statement ,如 [stmt.block] ,在这种情况下,OP称为“括号内”( block ):
而 ctor-initializer ,按,特别允许仅适用于构造函数的特殊功能[重点 mine]:
此形式,我们可以回答OP的问题。
,根据上述成员初始化器,正式 mem-initializer-list 是构造函数的功能体。
正确。
正确,正式的 mem-initializer-list (而
:age(pag)
是 ctor-initializer并非完全正确:定义也是声明。 [dcl.fct] 简单的术语,
person(int page);
是一个不是定义的声明,尤其是在此处省略函数与。不正确。函数的主体,如上所涵盖的,也可以选择地是a ctor-initializer 。在OP的示例中,
:age(page){}
是构造函数的函数。As per [dcl.fct.def.general], which tells us the grammar of a function definition, a ctor-initializer is part of the function-body:
The compound-statement, as per [stmt.block], is, in this context, what OP refers to as "within braces" (block):
Whereas ctor-initializer, as per [class.base.init], is particularly allowed only for the special kind of functions that are constructors [emphasis mine]:
With this, we can answer the OP's questions.
Yes, as per the above the member initializer, formally mem-initializer-list, is part of the function-body of the constructor.
Correct.
Correct, formally the mem-initializer-list (whereas
: age(pAge)
is the ctor-initializerNot entirely correct: a definition is also a declaration. [dcl.fct] describe the rules of function declarations, and in simple terms,
Person(int pAge);
is a declaration that is not a definition, particularly here by omission of a function-body.Incorrect. The body of a function, as covered above, container also, optionally, a ctor-initializer. In OP's example,
: age(pAge) {}
is the function-body of the constructor.是的,如构造函数和成员初始化列表:
在第二个问题的第四点中,构造函数的身体还包括上述成员初始化器列表。
Yes, as from Constructors and member initializer lists:
In the 4th point of your second question, the constructor's body also include the member initializer list as quoted above.
在您的示例中,年龄(页面)是实施的一部分,而不是声明。如果将其更改为年龄(第+页+1),则呼叫者将不会更改。构造函数的实现将调用基本构造函数,然后将默认的构造函数或诸如Age(页面)之类的构造函数,不记得订单,然后是构造函数的源代码。我认为语法有些奇怪。
In your example, age(pAge) is part of the implementation, not the declaration. If you changed it to age(pAge+1), callers wouldn’t change. The implementation of the constructor will call the baseclass constructor, then default constructors or constructors like age(pAge), can’t remember the order, then the compiled source code of the constructor. I’d consider the syntax a bit strange.