简体   繁体   English

基本类型的统一初始化?

[英]Uniform initialization for fundamental types?

Is it a good practice to use these definitions for fundamental types? 将这些定义用于基本类型是否是一种好习惯?

int num1(), num2(0);
char ch1(), ch2(' ');

They can be easily mistaken for function definitions. 它们很容易被误认为函数定义。

int nam1();
char ch1();

Because C++ lets you place variable declarations at any point in the program...I think there's little excuse for uninitialized variables. 因为C ++允许您将变量声明放置在程序中的任何位置...我认为未初始化变量几乎没有任何借口。

There is a bit of controversy regarding the difference in C++11 between Type var (param); 关于C ++ 11中Type var (param);之间的差异,存在一些争议Type var (param); and Type var {param}; 然后输入Type var {param}; with the latter being prescribed via what is called Uniform Initialization . 后者是通过所谓的统一初始化来规定的。 I've tried to only use uniform initialization and gotten some weird edge cases out of it. 我试图只使用统一初始化,并从中得到一些奇怪的情况。 I'm not sure it's all it's cracked up to be, and have been backing off and generally seeking code clarity. 我不确定这就是破解的全部内容,是否已经退缩并通常寻求代码清晰度。

So I would say char ch1(' '); 所以我会说char ch1(' '); , by virtue of being ugly, is better said as char ch = ' '; 由于丑陋,最好说成char ch = ' '; It really is a system of tradeoffs. 这确实是一个权衡的系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM