调用构造函数可以认为是函数声明? [英] call to constructor can be considered as function declaration?

查看:199
本文介绍了调用构造函数可以认为是函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来几行我要写的是The C ++ Standard Library - A tutorial and reference一书。

The next few lines I'm going to write come from the book "The C++ Standard Library - A tutorial and reference".


使用标准输入初始化:

Initialize by using standard input:



//read all integer elements of the deque from standard input
std::deque<int> c((std::istream_iterator<int>(std::cin)),
(std::istream_iterator<int>()));




不要忘记初始化程序参数附加的括号
这里。否则,这个表达式做的事情非常不同,你
可能会在后面的
语句中得到一些奇怪的警告或错误。考虑写下没有额外括号的语句:

Don't forget the extra parentheses around the initializer arguments here. Otherwise, this expression does something very different and you probably will get some strange warnings or errors in following statements. Consider writing the statement without extra parentheses:



std::deque<int> c(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>());




在这种情况下,c声明一个返回类型为
deque。它的第一个参数是类型istream_iterator,
名为cin,它的第二个未命名参数是类型function
,没有参数返回istream_iterator。此构造
在语法上作为声明或表达式有效。所以,
根据语言规则,它被当作一个声明。额外的
括号强制初始值设定程序不符合
声明的语法。

In this case, c declares a function with a return type that is deque. Its first parameter is of type istream_iterator with the name cin, and its second unnamed parameter is of type "function taking no arguments returning istream_iterator." This construct is valid syntactically as either a declaration or an expression. So, according to language rules, it is treated as a declaration. The extra parentheses force the initializer not to match the syntax of a declaration.

具有额外括号的那个不被认为是一个函数声明,但
我不能看到什么会使一个没有一个函数声明虽然?因为它在(std :: cin)周围有括号,并且据我所知,变量可能没有带括号的名称?

I can see why the one with extra parentheses is not considered a function declaration, but I can't see what would make the one without into a function declaration though? For it has parentheses around (std::cin), and as far as I know variables may not have names with parentheses?

我错过了什么?

推荐答案

这:

The tutorial you read is wrong. This:

std::deque<int> c(std::istream_iterator<int>(std::cin), std::istream_iterator<int>());

无法解析为函数声明,因为 std: :cin 不能是参数的名称。如果您删除 std 限定符:

Can't be parsed as a function declaration because std::cin can't be a name of a parameter. If you remove the std qualifier though:

std::deque<int> c(std::istream_iterator<int>(cin), std::istream_iterator<int>());

然后即可得到函数声明。


[...]据我所知,变量可能没有带括号的名称?

[...] and as far as I know variables may not have names with parentheses?

括号不是名称的一部分。你可以把它们放在那里,你喜欢多少:

The parentheses aren't part of the name. You can just put them there, how many you like:

int ((((((a)))))) = 12345; // valid code!
a++; // the variable is named 'a'

这篇关于调用构造函数可以认为是函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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