as I know in c++ when you pass a variable by reference means that we pass the very variable not a copy of it. so if a function that takes a refernce as a parameter we know that any change that that function does on the parameter will affect the original variable(variable passed in). but I'm stuck now: I have a member function that takes a reference to int this member function void DecrX(int &x) decrements x when it is called. the problem i get is that the original variable always never affected???!!! eg:
#include <iostream> using namespace std; class A { public: A(int &X):x(X){} int &getX(){return x;} void DecrX(){--x;} void print(){cout<<"A::x= "<<x<<endl<<endl;} private: int x; }; int main() { int x=7; cout<<"x= "<<x<<endl; A a(x);//we passed the x by reference a.DecrX();// here normally DecrX() affect the original x a.print();//here it is ok as we thought a.DecrX(); a.DecrX(); a.print(); cout<<"x= "<<x<<endl;//why x is still 7 not decremented cout<<endl; return 0; }