在构造函数中使用类成员名称
我知道这一点,因为这会将构造函数参数分配给类成员:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
That Thing 在 C++ 中称为成员初始值设定项列表。
使用初始值设定项列表初始化成员(第二个示例)与在构造函数体内为其赋值(第一个示例)之间存在差异。
当您通过初始值设定项列表初始化字段时,构造函数将被调用一次。使用传递的参数构造对象。
如果您使用赋值,那么字段将首先使用默认构造函数初始化,然后使用实际值重新分配(通过赋值运算符)。
正如您所看到的,创建和创建有额外的开销。后者的赋值对于用户定义的类来说可能相当重要。
构造事物(我不知道它是如何命名的)后列表中的名称如何解析?
在上面的片段中,有两个名为
B<的实体/code>:
A
的成员,在
A
的构造函数中作为参数接收的变量作为构造B
的参数传递(通过调用其构造函数),它是类A
的成员。这里的名称没有歧义,构造函数的全部内容是:this
是class A
指针。B()
是B
类型的构造函数。B
是B
类型的实例。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?
In the above snipet, there are two entities named
B
:A
The variable received as argument in Constructor of
A
gets passed as an argument for constructingB
(by calling its constructor) which is member of classA
. There is no ambiguity in names here, the all to constructor is:this
isclass A
pointer.B()
is constructor of typeB
.B
inside the parenthesis is an instance of typeB
.它将把参数分配给类成员。
这是初始化器列表,尽管该示例可能会导致混乱,但括号左侧的 id 是类的成员,而括号内的 id(或文字)只能是构造函数的参数之一: 这样想的话,就没有什么歧义了。
这里的关键是这个列表必须用一些值初始化类成员,所以如果你认为
在概念上等同于构造函数调用:
...没有歧义。
It will assign the parameter to the class member.
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
as conceptually equivalent to a constructor call:
... there is no ambiguity.
在第一种情况下,您的对象将被构造。在第二个中,它将被构造(使用默认构造函数)并分配。
您可能不需要赋值(可能未定义运算符,或者没有默认构造函数,或者行为是特定的,或者在某些情况下可能是由于性能问题)。
关于可见性,在这两种情况下,如果您只使用 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.