[cs23021-2] increment with pass-by-reference parameters

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Feb 24 11:53:50 EST 2010


CSI students,

Following yesterday's in-class discussion, I wrote the below test
program with a function accepting a single parameter by reference and
then tried to pass it an increment and decrement.

Out of the tree variants specified below, only prefix form of the
increment compiled and run. Exactly why this happens is a bit
technical and outside the scope of this course. So for now, the rules
are: only variables are allowed for pass-by-reference.

Thanks,
-- 
Mikhail

----------------
// demonstrating usage of variables with pass-by-reference parameters
// Mikhail Nesterenko
// 2/24/2010

#include <iostream>

using namespace std;

void testFunc(int&);

int main(){
   int i=0;
   // testFunc(i++);  // does not work
   // testFunc(i+1);  // does not work
   testFunc(++i);
   cout << "i = " << i << endl;
}

void testFunc(int& j){
   cout << "j =" << j << endl;
   return;
}


More information about the cs23021-2 mailing list