Use the following code to change the private variable outside of a member function definition:
class test
{
int x;
public:
test():x(0){}
int& getx(){return x;}
};
int main()
{
test t(10);
cout<<t.getx()<<endl; //x = 10
int& i = t.getx();
i = 20;
cout<<t.getx(); //x = 20 (value of x changed)
return 0;
}