class - How to access a member variable without instantiation, C++ -
suppose have class b gets value 'v' in constructor class a. how can read value class c? class c instantiated on demand, has created b , passed 'v' already. 'v' change in every instantiation. have tried make 'v' static in class b. work? not implement properly.
class { public: int* v; b b1; a(int* var) : v(var), b1(var) {}; }
how access same version of 'v' c class? can define b , c in order achieve goal. cannot change purpose.
you cannot access 'v' has never passed class. instead can make static copy of member in class a. update every time instantiated this:
class { public: int* v; static int* staticv; ...// constructor etc }
in .cc code of a:
int* a::staticv; ... a::staticv=this->v;
now class can access value by:
a::staticv;
Comments
Post a Comment