变量可以在声明的同一行上用istream初始化吗? [英] Can a variable be initialized with an istream on the same line it is declared?
本文介绍了变量可以在声明的同一行上用istream初始化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下两行可以压缩为一个吗?
Can the following two lines be condensed into one?
int foo;
std::cin >> foo;
推荐答案
智能资产答案:
int old; std::cin >> old;
可怕的答案:
int old, dummy = (std::cin >> old, 0);
正确答案: old
用声明定义,然后才能将其传递给 operator>>
作为参数。在变量的声明中获得函数调用的唯一方法是将其放在上述的初始化表达式中。可接受的声明变量并读取输入内容的方式与您所写的相同:
The proper answer: old
has to be defined with a declaration before it can be passed to operator>>
as an argument. The only way to get a function call within the declaration of a variable is to place it in the initialization expression as above. The accepted way to declare a variable and read input into it is as you have written:
int old;
std::cin >> old;
这篇关于变量可以在声明的同一行上用istream初始化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文