In C++, how can I accept a variable by reference, then store it on the object instance - so that I can modify it any time I want?
The variable in question is a volatile unsigned char
currently.
An example class to clarify:
class SomeClass {
public:
SomeClass(volatile unsigned char &value);
void changeIt();
private:
volatile unsigned char _value;
}
SomeClass::SomeClass(volatile unsigned char &value) {
_value = value;
}
void SomeClass:changeIt() {
_value++;
}
What I now want is to let the caller be able to pass this variable, and let me change it whenever I need. The reason I need this is that the passed variable is special (it maps to a CPU registry on a microcontroller).
I want to be able to do this:
SomeClass myClass(PORTB);
// PORTB is the same
myClass.changeIt();
// PORTB is changed
I'm pulling my hair out trying to figure this out (I come from the Java world... :)