是成员初始化列表,被视为构造函数主体的一部分,或者它被视为声明器的一部分

发布于 2025-01-23 14:18:48 字数 1171 浏览 0 评论 0 原文

我正在学习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) 正式 是构造函数的一部分。我的意思是,我已经读到,函数的身体从开口 {开始,并在关闭} 上结束。据我当前的理解,这里涉及四件事:

  1. ctor定义:这包括整个
//this whole thing is ctor definition
Person(int pAge): age(pAge)
        {
        }
  1. 成员ritializer :这是 age(page)< /代码>部分。

  2. ctor声明:这是 person(int page) part。

  3. 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:

  1. Ctor definition: This includes the whole
//this whole thing is ctor definition
Person(int pAge): age(pAge)
        {
        }
  1. Member initializer: This is the age(pAge) part.

  2. Ctor declaration: This is the Person(int pAge) part.

  3. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒与心事 2025-01-30 14:18:48

按照

 功能定义:
    [...]功能体

功能体:
    ctor-initializer_opt化合物
 

compound-statement ,如 [stmt.block] ,在这种情况下,OP称为“括号内”( block ):

复合语句(也称为块)分组
语句中的单个语句。

 化合物:
    {statement-seq_opt}
 

ctor-initializer ,按,特别允许仅适用于构造函数的特殊功能[重点 mine]:

的构造函数的定义中
直接和虚拟基类亚对象和非静态数据成员
可以由具有表格

的A ctor-initializer 指定

  ctor-Initializer:
:mem-Initializer-list
 

此形式,我们可以回答OP的问题。


是成员初始化列表,被视为构造函数正文的一部分,或者它被认为是声明器的一部分

,根据上述成员初始化器,正式 mem-initializer-list 构造函数的功能体。


我的第二个问题是上面给定的描述正确吗?

1。 CTOR定义:这包括整个

  //整个过程是CTOR定义
人(int页):年龄(页)
        {
        }
 

正确。

2。成员初始化器:这是 age(page) part。

正确,正式的 mem-initializer-list (而:age(pag) ctor-initializer

3。 CTOR声明:这是 person(int页) part。

并非完全正确:定义也是声明。 [dcl.fct] 简单的术语, person(int page); 是一个不是定义的声明,尤其是在此处省略函数与

4。 CTOR的身体:这是开口 {和关闭}

之间的区域

不正确。函数的主体,如上所涵盖的,也可以选择地是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:

function-definition:
    [...] function-body

function-body:
    ctor-initializer_opt compound-statement

The compound-statement, as per [stmt.block], is, in this context, what OP refers to as "within braces" (block):

A compound statement (also known as a block) groups a sequence of
statements into a single statement.

compound-statement:
    { statement-seq_opt }

Whereas ctor-initializer, as per [class.base.init], is particularly allowed only for the special kind of functions that are constructors [emphasis mine]:

In the definition of a constructor for a class, initializers for
direct and virtual base class subobjects and non-static data members
can be specified by a ctor-initializer, which has the form

ctor-initializer:
: mem-initializer-list

With this, we can answer the OP's questions.


Is member initializer list considered part of the body of a constructor or it it considered part of the declarator

Yes, as per the above the member initializer, formally mem-initializer-list, is part of the function-body of the constructor.


My second question is that is the above given description correct?

1. Ctor definition: This includes the whole

//this whole thing is ctor definition
Person(int pAge): age(pAge)
        {
        }

Correct.

2. Member initializer: This is the age(pAge) part.

Correct, formally the mem-initializer-list (whereas : age(pAge) is the ctor-initializer

3. Ctor declaration: This is the Person(int pAge) part.

Not 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.

4. Ctor's body: This is the region between the opening { and the closing }.

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.

绿萝 2025-01-30 14:18:48

成员初始化器 age(page)正式的构造函数的一部分?

是的,如构造函数和成员初始化列表

在化合物语句的开头之前,任何构造函数的函数定义的主体都可以包括成员初始化器列表 ,其语法是结肠字符:一个或多个成员界的列表,...


上面给定的描述正确吗?

在第二个问题的第四点中,构造函数的身体还包括上述成员初始化器列表。

is the member initializer age(pAge) formally part of the constructor's body?

Yes, as from Constructors and member initializer lists:

The body of a function definition of any constructor, before the opening brace of the compound statement, may include the member initializer list, whose syntax is the colon character :, followed by the comma-separated list of one or more member-initializers,...


is the above given description correct?

In the 4th point of your second question, the constructor's body also include the member initializer list as quoted above.

彼岸花似海 2025-01-30 14:18:48

在您的示例中,年龄(页面)是实施的一部分,而不是声明。如果将其更改为年龄(第+页+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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文