struct C
{
int number;
void addOne(){number++;}
C():a(0){}
}
struct B
{
};
struct A
{
B b;
}
I'm trying to be able to instantiate 1 instance of C and any number of A and B. From there I want to be able to call C's addOne() function from inside of A or B and have it add 1 to the same variable "number" every time.
Is this possible to do without just re-writing the class as part of main()?
class C
{
privatestatic C* _self;
protected:
C(){}
public:
static C* Instance()
{
if(!_self) _self = new C();
return _self;
}
void addOne(){number++;}
int number; //left it public like in your code
};
//usage
C::Instance() -> addOne();
Or you can just make usage of static variables and do not create instances at all:
1 2 3 4 5 6 7 8
struct C
{
staticint number = 0;
staticvoid addOne(){number++;}
};
//usage
C::addOne();