references must be initialised when created, and cannot be changed. They are effectively and alias to the "real" variable.
>>Reference set to outside variable
Concept: Shouldn't classes only change their own data?
Why do want to amend a global variable through the use of a referene anyway?
Anyways, here's one way it might work for you:
Code:
#include <iostream>
using namespace std;
int x;
class MyClass
{
public:
MyClass(void);
void ChangeIt(int);
int &y;
};
MyClass::MyClass(void) :
y(x)
{
}
void MyClass::ChangeIt(int num)
{
y = num;
}
int main(void)
{
x = 11;
MyClass c;
cout << "x is " << x << endl;
c.ChangeIt(12);
cout << "x is " << x << endl;
}
/*
Output:
x is 11
x is 12
*/
Doh! Been beaten, but I'm going to post this anyway!