C++,通过引用变量传递,未在调用函数的同一行更新
在我的 C++ 程序中,我有这个函数,
char MostFrequentCharacter(ifstream &ifs, int &numOccurances);
在 main() 中,是这段代码,
ifstream in("file.htm");
int maxOccurances = 0;
cout <<"Most freq char is "<<MostFrequentCharacter(in, maxOccurances)<<" : "<<maxOccurances;
但这不起作用,尽管我获得正确的字符, maxOccurance 保持为零。 但是如果我用这个替换上面的 main 代码,
ifstream in("file.htm");
int maxOccurances = 0;
char maxFreq = MostFrequentCharacter(in, maxOccurances);
cout <<"Most freq char is "<<maxFreq<<" : "<<maxOccurances;
那么它就可以正常工作。我的问题是为什么它在第一种情况下不起作用。
In my c++ program, I have this function,
char MostFrequentCharacter(ifstream &ifs, int &numOccurances);
and in main(), is this code,
ifstream in("file.htm");
int maxOccurances = 0;
cout <<"Most freq char is "<<MostFrequentCharacter(in, maxOccurances)<<" : "<<maxOccurances;
But this is not working, though I am getting the correct char, the maxOccurance remains zero.
But if I replace the above code in main with this,
ifstream in("file.htm");
int maxOccurances = 0;
char maxFreq = MostFrequentCharacter(in, maxOccurances);
cout <<"Most freq char is "<<maxFreq<<" : "<<maxOccurances;
Then, it is working correctly. My question is why is it not working in first case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为在第一种情况下,表达式中 maxOccurances 的值是在调用 MostFrequentCharacter 之前解析的。但它不一定是这样,它是未指定的行为。
使用不同的编译器或编译器选项,您可能会遇到不同的结果。例如,如果您在 VC++ 上尝试同样的事情,我相信您会看到不同的结果。
Because in the first case, the value of maxOccurances in the expression is being resolved before the call to MostFrequentCharacter. It doesn't have to be that way though, it is unspecified behavior.
You may experience different results with different compilers, or compiler options. If you try that same thing on VC++ for example, I believe you will see different results.
您只需注意,在您看到
<<
的地方,您实际上是在调用operator<<
方法 - 因此编译器正在计算参数的值在修改变量之前传递到该函数。换句话说,您所拥有的类似于
...并且由于函数参数的求值顺序未定义,因此它取决于编译器。
You just have to note that where you see
<<
you are actually calling theoperator<<
method - so the compiler is working out the value of the arguments to pass into that function before your variable is modified.In other words, what you have is similar to
...and since the evaluation order of function arguments is undefined, it depends on the compiler.
Cout 在编译器中从右到左工作,因此首先评估最右边的,然后评估左边的。 :)
所以引用变量的值没有改变。
Cout works right to left in your compiler so first rightmost is evaluated then left one. :)
So the value of referenced variable isn't changed.
在 C++ 中,
按关联性计算结果为:
但编译器可以自由地以任何顺序计算它们。
即,编译器可以首先计算
b
,然后计算a
,然后是第一个<<
操作和第二个<
操作。 < 操作。这是因为没有与<<
关联的序列点为了简单起见,让我们考虑以下代码,它是等效的:
在上面的源代码中:
求值为函数调用:
在这个中函数调用是否首先评估运算符 <<(std::cout,i) 还是
i++
是未指定。 即:operator<<(std::cout,i)
可能首先评估或者i++
可能首先评估或者编译器实现的一些魔法排序
鉴于上述情况,无法定义此排序,因此也无法进行解释。
C++03 标准的相关引用:
第 1.9 节
In C++,
By Associativity evaluates to:
but the compiler is free to evaluate them in any order.
i.e, the compiler can evaluate
b
first, thena
, then the first<<
operation and the the second<<
operation. This because there is no sequence point associated with<<
For the sake of simplicity let us consider the following code, which is equivalent:
In the above source code:
evaluates to the function call:
In this function call whether
operator<<(std::cout,i)
ori++
gets evaluated first is Unspecified. i.e:operator<<(std::cout,i)
maybe evaluated first Ori++
maybe evaluated first OrSome Magic Ordering implemented by the compiler
Given the above, that there is no way to define this ordering and hence no explanation is possible either.
Relevant Quote from the C++03 Standard:
Section 1.9