I need to save in Class static ref to a variable for use in nested classes in a future.
I have no problem in use ref to a variable (not static), but with static i have to use int & myClass::refA = ...
in someware.
The problem is that i can use int & myClass::refA = ...
only in myClass constuctor, because only there i got the ref for refA, in myClass mc(newA)
colling.
How to initialize a static reference to a variable inside the class constuctor?
Code:
class myClass
{
private:
static int & refA;
public:
myClass(int &_refA);
void updateA();
};
myClass::myClass(int &_refA)
: refA(_refA)
{
}
void myClass::updateA() {
refA = refA++;
}
int newA = 10;
myClass mc(newA);
mc.updateA();
Error:
In constructor 'myClass::myClass(int&)':
QF:17:3: error: 'int& myClass::refA' is a static data member; it can only be initialized at its definition
: refA(_refA)
^
exit status 1
'int& myClass::refA' is a static data member; it can only be initialized at its definition