I developed a series of related classes, and in order to be able to manage them, I made them stem from a single abstract class.
These classes all need access to a series of shared resources, and I found that I create a vector of pointers in each, they are all identical (they must be). It seems that creating a static member in the base class would give all derived classes access to this vector, that is, I only needed to build it once (it will not change even after it was built, just looked up).
My question is that this is normal, and if so, how can I then build it without calling the "fill vector" method from one of the derived classes?
My thinking was to do something like
class Resource {}; enumR {RES0, RES1}; class AbstractClass { public: virtual void OnInit() = 0; void static fillVector(Resource* pResource, enumR Resourcename) {lResource[Resourcename]=pResource;}; protected: static vector<Resource*> lResource; }; vector<Resource*> AbstractClass::lResource; int main() { Resource res0, res1; AbstractClass::fillVector(&res0, RES0); AbstractClass::fillVector(&res1, RES1); return 0; };
Then, when I instantiate an object of any class derived from AbstractClass, I will have access to the lResource vector that I want.
Will this work? It's horrible? This is normal?
source share