[英]C++ object creation and constructor
I'm learning ctors now and have a few question.我现在正在学习 ctors 并且有一些问题。 At these lines:在这些行:
Foo obj(args);
Foo obj2;
obj2 = Foo(args);
Foo obj3 = Foo(args);
First part : only 1 constructor called (Foo) and obj
is initialized.第一部分:只有 1 个称为 (Foo) 和obj
构造函数被初始化。 So, 1 object creation.因此,创建 1 个对象。
Second part : creating of temporary object obj2
, calling default ctor for it.第二部分:创建临时对象obj2
,为其调用默认构造函数。 Next lines we create another copy of Foo
and pass its copy into operator=()
.接下来的几行我们创建Foo
另一个副本并将其副本传递给operator=()
。 Is that right?是对的吗? So, 3 local temporary objects, 2 constructor callings.因此,3 个本地临时对象,2 个构造函数调用。
Third part : create 1 object Foo
and pass its copy into operator=()
.第三部分:创建 1 个对象Foo
并将其副本传递给operator=()
。 So, 2 temprorary objects and 1 ctor calling.因此,2 个临时对象和 1 个 ctor 调用。
Do I understand this right?我理解正确吗? And if it's true, will compiler (last gcc, for example) optimize these in common cases?如果这是真的,编译器(例如最后一个 gcc)会在常见情况下优化这些吗?
I will comment on the third one first:我先评论第三个:
Foo obj3=Foo(args);
It doesn't use operator=
which is called copy-assignment.它不使用被称为复制赋值的operator=
。 Instead it invokes copy-constructor (theoretically).相反,它调用复制构造函数(理论上)。 There is no assignment here.这里没有任务。 So theoretically, there is two objects creation, one is temporary and other is obj3
.所以理论上,有两个对象创建,一个是临时的,另一个是obj3
。 The compiler might optimize the code, eliding the temporary object creation completely.编译器可能会优化代码,完全消除临时对象的创建。
Now, the second one:现在,第二个:
Foo obj2; //one object creation
obj = Foo(args); //a temporary object creation on the RHS
Here the first line creates an object, calling the default constructor.这里第一行创建一个对象,调用默认构造函数。 Then it calls operator=
passing the temporary object created out of the expression Foo(args)
.然后它调用operator=
传递从表达式Foo(args)
创建的临时对象。 So there is two objects only the operator=
takes the argument by const
reference (which is what it should do).所以有两个对象,只有operator=
通过const
引用获取参数(这是它应该做的)。
And regarding the first one, you're right.关于第一个,你是对的。
Yes, Foo obj(args)
creates one Foo object and calls the ctor once.是的, Foo obj(args)
创建一个 Foo 对象并调用一次ctor。
obj2
is not considered a temporary object. obj2
不被视为临时对象。 But just like 1 Foo obj2
creates one object and calls the Foo
ctor.但是就像 1 Foo obj2
创建一个对象并调用Foo
ctor。 Assuming that you meant obj2 = Foo(args)
for the next line, this line creates one temporary Foo object and then calls obj2.operator=()
.假设你的意思是下一行obj2 = Foo(args)
,这一行会创建一个临时 Foo 对象,然后调用obj2.operator=()
。 So for this second example there is only a single temporary object, a single non-temporary, Foo ctors are called twice (once for the non-temporary, once for the temporary) and the operator=() is called once.因此,对于第二个示例,只有一个临时对象,一个非临时对象,Foo ctors 被调用两次(一次用于非临时,一次用于临时)并且 operator=() 被调用一次。
No, this line does not call operator=()
.不,这一行不调用operator=()
。 When you initialize obj3
using the =
syntax it is almost exactly as if you had used parentheses instead: Foo obj3(Foo(args));
当您使用=
语法初始化obj3
,它几乎就像您使用括号一样: Foo obj3(Foo(args));
So this line creates a temporary object, and then calls the Foo copy ctor to initialize obj3 using that temporary object.所以这一行创建了一个临时对象,然后调用 Foo 复制构造函数来使用该临时对象初始化 obj3。
Your terminology is a bit confusing.你的术语有点混乱。
The objects obj
, obj2
obj3
are not called "temporary objects".对象obj
、 obj2
obj3
不被称为“临时对象”。 Only the instance that is created in line 3 before being assign to obj is a temporary object.只有在分配给 obj 之前在第 3 行中创建的实例才是临时对象。
Also, you don't create "a copy of Foo", you create either "an instance of Foo" or "an object of type Foo".此外,您不会创建“Foo 的副本”,而是创建“Foo 的实例”或“Foo 类型的对象”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.