在C ++中显式和隐式赋值之间有什么区别 [英] What's the difference between explicit and implicit assignment in C++

查看:825
本文介绍了在C ++中显式和隐式赋值之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int value = 5; // this type of assignment is called an explicit assignment
int value(5); // this type of assignment is called an implicit assignment

以及在什么情况下,显式和隐式赋值不同,以及如何?

What is the difference between those, if any, and in what cases do explicit and implicit assignment differ and how?

http://weblogs.asp.net/kennykerr/archive/2004/08/31/Explicit-Constructors.aspx

编辑:我实际上刚刚发现这篇文章,这使得整个事情更清楚...它带来了另一个问题,如果你( )标记构造函数接受一个基本类型的单个参数 - numeric / bool / string - 作为显式,并保留其余的原样(当然保持观察像$ ,SomeType = SomeType())

I actually just found this article, which makes the whole thing a lot clearer... and it brings up another question, should you (in general) mark constructors taking a single parameter of a primitive type - numeric/bool/string - as explicit and leave the rest as they are (of course keeping watch for gotchas such as constructors like (int, SomeType = SomeType())?

推荐答案

显示,然后其中一个不起作用。

They differ if a class has a constructor marked 'explicit'. Then, one of these does not work.

否则没有区别。

这篇关于在C ++中显式和隐式赋值之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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