在构造函数中使用类成员名称

发布于 2024-12-04 21:13:19 字数 383 浏览 0 评论 0原文

我知道这一点,因为这会将构造函数参数分配给类成员:

class A
{
    public:
        some_type B;
        A(some_type B)
        {
            this->B = B;
        }
}

但这会做什么:

class A
{
    public:
        some_type B;
        A(some_type B) : B(B)
        {
        }
}

这会将参数分配给自身还是将参数分配给类成员或执行其他操作?
构造事物(我不知道它是如何调用的)后列表中的名称如何解析?

I know this as this will assign the constructor parameter to the class member:

class A
{
    public:
        some_type B;
        A(some_type B)
        {
            this->B = B;
        }
}

But what will this do:

class A
{
    public:
        some_type B;
        A(some_type B) : B(B)
        {
        }
}

Will this assign the parameter to itself or the parameter to the class member or do something else?
How are the names in the list after construct thing (I have no idea how its called) resolved?

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

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

发布评论

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

评论(3

维持三分热 2024-12-11 21:13:20

That Thing 在 C++ 中称为成员初始值设定项列表

使用初始值设定项列表初始化成员(第二个示例)与在构造函数体内为其赋值(第一个示例)之间存在差异。

当您通过初始值设定项列表初始化字段时,构造函数将被调用一次。使用传递的参数构造对象。

如果您使用赋值,那么字段将首先使用默认构造函数初始化,然后使用实际值重新分配(通过赋值运算符)。

正如您所看到的,创建和创建有额外的开销。后者的赋值对于用户定义的类来说可能相当重要。


构造事物(我不知道它是如何命名的)后列表中的名称如何解析?

public:
    some_type B;
    A(some_type B) : B(B)
    {
    }

在上面的片段中,有两个名为B<的实体/code>:

  1. 第一个是构造函数作为参数接收的一个&
  2. 第二个是类 A 的成员,

A 的构造函数中作为参数接收的变量作为构造 B 的参数传递(通过调用其构造函数),它是类 A 的成员。这里的名称没有歧义,构造函数的全部内容是:

this->B(B);
  1. thisclass A 指针。
  2. B()B 类型的构造函数。
  3. 括号内的BB类型的实例。

That Thing is called a Member Initializer List in C++.

There is a difference between Initializing a member using initializer list(2nd Example) and assigning it an value inside the constructor body(1st Example).

When you initialize fields via initializer list the constructors will be called once. The object gets constructed with the passed parameters.

If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.


How are the names in the list after construct thing (I have no idea how its called) resolved?

public:
    some_type B;
    A(some_type B) : B(B)
    {
    }

In the above snipet, there are two entities named B:

  1. First is the one that constructor receives as an argument &
  2. Second is the member of the class A

The variable received as argument in Constructor of A gets passed as an argument for constructing B(by calling its constructor) which is member of class A. There is no ambiguity in names here, the all to constructor is:

this->B(B);
  1. this is class A pointer.
  2. B() is constructor of type B.
  3. B inside the parenthesis is an instance of type B.
可是我不能没有你 2024-12-11 21:13:20

这会将参数分配给自身还是将参数分配给类
会员还是做其他事情?

它将把参数分配给类成员。

构建后列表中的名称如何(我不知道
它是如何被称为)解决的?

这是初始化器列表,尽管该示例可能会导致混乱,但括号左侧的 id 是类的成员,而括号内的 id(或文字)只能是构造函数的参数之一: 这样想的话,就没有什么歧义了。

这里的关键是这个列表必须用一些值初始化类成员,所以如果你认为

...: B(B)

在概念上等同于构造函数调用:

this->B(B)

...没有歧义。

Will this assign the parameter to itself or the parameter to the class
member or do something else?

It will assign the parameter to the class member.

How are the names in the list after construct thing (I have no idea
how its called) resolved?

This is the initializers list, and though the example can lead to confusion, the id at the left of the parenthesis is a member of the class, while the id (or literal) inside the parenthesis can only be one of the parameters of the constructor: thought that way, there is no ambiguity at all.

The key here is that this list must initialize class members with some value, so if you think of

...: B(B)

as conceptually equivalent to a constructor call:

this->B(B)

... there is no ambiguity.

夏の忆 2024-12-11 21:13:20

在第一种情况下,您的对象将被构造。在第二个中,它将被构造(使用默认构造函数)并分配。

您可能不需要赋值(可能未定义运算符,或者没有默认构造函数,或者行为是特定的,或者在某些情况下可能是由于性能问题)。

关于可见性,在这两种情况下,如果您只使用 B,那么它就是您正在操作的参数。

In the first situation, your objet will be constructed. In the second, it will be constructed (with default constructor) and assigned.

You might not want assignation (maybe the operator is not defined, or no default constructur or the behaviour is specific, or maybe, in some cases because of performance issues).

Concerning the visibility, in both cases, if you just use B, it's the parameter you're manipulating.

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