I'm declaring a variable within
int main(), and then, in one line, altering its value by passing it by reference to a function, then outputting it via
cout <<. Only problem is,
cout <<is printing the original initialized value of the variable to the screen, rather than the newly altered version. I have come up with a solution, but I don't like it. Actually, the code is simple enough that I can post it (below). It's a simple Half Adder function. Take two binary inputs, output the result and the carry. I pass the carry by reference, and have the function return the result of the addition.
#include <iostream>
using namespace std;
bool HalfAdder(bool, bool, bool&);
int main(int argc, char ** argv){
bool input[] = {true, true};
bool carry = false;
cout << "Result is " << HalfAdder(input[0], input[1], carry) << " carry is " << carry << endl;
system("PAUSE");
return 0;
}//end of main
bool HalfAdder(bool a, bool b, bool& carry){
if( a & b ){
//1 + 1 = 0, carry = 1
carry = true;
return false;
}//end of if
else{
//1 + 0 = 1, no carry. No carry with 0 + 0 either
carry = false;
//1 + 0 = 1; 0 + 0 = 0
return (a || b) ? true : false;
}//end of else
}//end of HalfAdder()
If you run this, unfortunately, carry is always 0. False. Because it's initialized as false. This, of course, despite the fact that I pass it by reference in the call. While I'm sure the C/C++ veterans have probably already scrolled down to the reply box by now, for those of you still reading, I'd like to offer a theory for some feedback (I'll do the research later tonight). I believe that the problem is that I'm passing HalfAdder as a parameter to the << member function of cout on the same line that I pass carry to another instance of <<.
So while I expected this sort of thing to happen:
cout.operator<<("Result is");
cout.operator<<(HalfAdder());
cout.operator<<("carry is");
cout.operator<<(carry);
What really happened is something like...
cout.operator<<("Result is", HalfAdder(), "carry is", carry);Not that exactly, but that basic concept. What I mean is, maybe all of the parameters I passed to << were copied immediately, and then HalfAdder() was executed, so that even though carry has been altered, it's too late; the original value of carry was copied already. I tested this out by running the same code but adding an extra line before
system("PAUSE").
cout << "Result is " << HalfAdder(input[0], input[1], carry) << " carry is " << carry << endl;
cout << "But the true carry is " << carry << endl;
system("PAUSE");
And, hey, it says the true carry is 1, as expected. That's actually pretty amazing. I never even thought of that. Oddly enough, I compiled this in Dev-C++ before handing it in for class, and I assume it worked fine, otherwise I probably wouldn't have handed it in. Is it even remotely possible this is a compiler-specific thing? My most recent build of it where I noticed the error was in MSVC++2012.
Anyway, thanks for your time. Sorry if this was too much too read. I just think it's a pretty cool error to have made.







