在c ++中调用构造函数而没有新的 [英] Calling constructors in c++ without new
本文介绍了在c ++中调用构造函数而没有新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我经常看到人们使用
Thing myThing("asdf");
而不是这样:
Thing myThing = Thing("asdf");
这似乎工作(使用gcc),至少没有涉及模板。我现在的问题是,第一行是否正确,如果是这样,我应该使用它吗?
This seems to work (using gcc), at least as long as there are no templates involved. My question now, is the first line correct and if so should I use it?
推荐答案
微妙地不同的东西。
第一行通过调用格式 Thing(const char *)
。
The first line creates a new object on the stack by calling a constructor of the format Thing(const char*)
.
第二个有点复杂。它基本上执行以下操作
The second one is a bit more complex. It essentially does the following
- 使用构造函数创建
Thing
code> Thing(const char *) - 使用
Thing
构造函数Thing(const Thing&)
- 调用
〜Thing()
在步骤#1中创建的对象
- Create an object of type
Thing
using the constructorThing(const char*)
- Create an object of type
Thing
using the constructorThing(const Thing&)
- Call
~Thing()
on the object created in step #1
这篇关于在c ++中调用构造函数而没有新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文