C ++,传递引用变量不会在调用函数的同一行上更新 [英] C++, pass by reference variable not being update on the same line on which function is called

查看:118
本文介绍了C ++,传递引用变量不会在调用函数的同一行上更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的c ++程序中,我有这个函数,



char MostFrequentCharacter(ifstream& ifs,int& numOccurances) code>



并且在main()中,是此代码,

  ifstream in(file.htm); 
int maxOccurances = 0;
cout<<最多频率字符是<<< MostFrequentCharacter(in,maxOccurances)<:<< maxOccurances;

但这不工作,虽然我得到正确的字符,maxOccurance保持为零。
但是如果我用main替​​换上面的代码,

  ifstream in(file.htm); 
int maxOccurances = 0;
char maxFreq = MostFrequentCharacter(in,maxOccurances);
cout<<最多频率char是<< maxFreq<<:<< maxOccurances;

然后,它正常工作。我的问题是为什么它不能在第一种情况下工作。

解决方案

在C ++中,

  cout<< a<< b 

By Associativity评估为:

 (cout<< a)< b 

但编译器可以按任意顺序自由评估。



即,编译器首先可以评估 b ,然后 a 操作,而第二 <<$ 操作。这是因为没有与<

相关联的顺序点




为了简单起见,让我们考虑下面的代码,它是等价的:

 #include< iostream> 
int main()
{
int i = 0;
std :: cout<< i<< i ++;
return 0;
}

在上述源代码中:

  std :: cout<< i<< i ++; 

计算函数调用:

  operator<<(operator< <(std :: cout,i),i ++); 

在此函数中调用是否 operator<<<(std :: cout, i) i ++ 首先被评估为未指定



运算符<<(std :: cout,i)可能先计算或

i ++ 可以先计算或者

编译器执行一些魔术排序



鉴于上面的情况,没有办法定义这个排序






C ++ 03标准的相关引用:

第1.9节


抽象机器的其他方面和操作在本国际标准中被描述为未指定例如,对函数的参数的求值顺序)。在可能的情况下,本国际标准定义了一组允许的行为。这些定义了抽象机的非确定性方面。



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.

解决方案

In C++,

cout << a << b 

By Associativity evaluates to:

(cout << a) << b 

but the compiler is free to evaluate them in any order.

i.e, the compiler can evaluate b first, then a, 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:

 #include<iostream>
    int main()
    {
       int i = 0;
       std::cout<<i<<i++;
       return 0;
    }

In the above source code:

std::cout<<i<<i++;

evaluates to the function call:

operator<<(operator<<(std::cout,i),i++);

In this function call whether operator<<(std::cout,i) or i++ gets evaluated first is Unspecified. i.e:

operator<<(std::cout,i) maybe evaluated first Or
i++ maybe evaluated first Or
Some 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

Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function). Where possible, this International Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the abstract machine.

这篇关于C ++,传递引用变量不会在调用函数的同一行上更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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