放牧代码和思想
专注自然语言处理、机器学习算法
    This thing called love. Know I would've. Thrown it all away. Wouldn't hesitate.

C++基本类型的显式初始化

以前遗漏了这一特性,在《C++标准程序库—自修教程与参考手册》看到相关讲解,做个笔记。

ExplicticInitialization,也即使用“内置”的初始化函数为基本类型提供初始值,比起使用int i2 = 0;这种魔术数字来来,使用int()也许是更优雅的选择。

#include <iostream>
using namespace std;

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
	int i1;
	cout << i1 << endl;
	int i2 = int();
	cout << i2 << endl;

	float f1;
	cout << f1 << endl;
	float f2 = float();
	cout << f2 << endl;

	system("pause");
	return 0;
}
///////////////////////////End Sub//////////////////////////////////
/************************************************************************/
/* output:
-858993460
0
-1.07374e+008
0
*/
/************************************************************************/

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » C++基本类型的显式初始化

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》