构造函数的快捷方式 [英] Shortcut for constructor

查看:180
本文介绍了构造函数的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我不知道如何定义这个成语。

Actually I don't know how to define this idioms.

在某些代码中我有红色的东西:

In some code i have red something like:

ClassWithAMessage c = "This is the message";

其中我希望阅读:

ClassWithAMessage c("This is the message");

我不知道如何重现这种行为,有人可以提供一些信息或玩具的例子吗?

I don't know how to reproduce this behavior, can someone provide some information or a toy example?

推荐答案

ClassWithAMessage c = "This is the message";

复制初始化。复制构造函数必须可用于此才能工作。首先,使用的转换构造函数构造一个临时 ClassWithAMessage 这是消息。然后,临时文件与复制构造函数一起构造 c 。这是受复制的影响(temp可能不存在)。

is copy initialization. A copy constructor must be available for this to work. First, a temporary ClassWithAMessage is constructed using the conversion constructor from "This is the message". The temporary is then used with the copy constructor to construct c. This is subject to copy elision (the temp might not be there).

ClassWithAMessage c("This is the message");

直接初始化。直接使用转换构造函数。

is direct initialization. The conversion constructor is used directly.

不是真正的成语,只是不同的方式来构造对象。

Not really idioms, just different ways to construct an object.

这篇关于构造函数的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆