简体   繁体   English

以下声明之间的区别是什么?

[英]What is the diff between following declarations?

vector<double> salaries();
vector<double> salaries;

I wanted to declare a empty vectors and above two declarations are possible as compiler returned no error. 我想声明一个空向量,并且上面的两个声明都是可能的,因为编译器未返回错误。 What is the difference between two above declarations? 以上两个声明之间有什么区别?

With the update, the two declarations you show are: 通过更新,您显示的两个声明是:

  1. vector<double> salaries(); vector <double> salaries(); // function named salaries that takes no parameters and returns a vector<double> //名为salaries的函数,不带任何参数并返回一个向量<double>
  2. vector<double> salaries; vector <double>工资; // a variable with type vector<double> that is default-constructed //默认构造的类型为vector <double>的变量

Note that (1) is sometimes written with the intention of doing the same thing as (2) (ie using the default constructor). 注意,有时写(1)的目的是和(2)做相同的事情(即使用默认构造函数)。 But that's not how the compiler sees it - this is often referred to as a "most vexing parse", which you should be able to find much more information on with a little searching. 但这不是编译器所看到的方式-这通常被称为“最烦人的解析”,您只需稍作搜索就可以找到更多的信息。 Basically, though, it's an (intentional) ambiguity in the language syntax, that is resolved by the language specs by requiring that particular syntax to be treated as a function, but still surprises a lot of people. 但是,基本上,这是语言语法中的(故意)模糊性,通过语言规范将要求将特定语法视为函数来解决,但仍然令很多人感到惊讶。

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

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