开发者

C++, pass by reference variable not being update on the same line on which function is called

开发者 https://www.devze.com 2023-03-27 14:11 出处:网络
In my c++ program, I have this function, char MostFrequentCharacter(ifstre开发者_C百科am &ifs, int &numOccurances);

In my c++ program, I have this function,

char MostFrequentCharacter(ifstre开发者_C百科am &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.


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.


You just have to note that where you see << you are actually calling the operator<< 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

operator<<(operator<<(cout, f(x)), x);

...and since the evaluation order of function arguments is undefined, it depends on the compiler.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消