class - C++: How to create a collection of classes at startup -


i'm using c++ 11. have n source files each containing class inherits common base class. @ startup, want each class register collection. registration include 1) data necessary identifying purpose of class, 2) class factory creating instances of class. number of source files unknown. pattern doing this? solution needs cross-platform compatible visual studio 2013, gcc , others.

first things first: keep in mind possible if classes derived single base class, because can store 1 type of object per vector instance.

and when comes solution... can declare object in corresponding *.cpp file (mark extern in *.h file):

// someclass.h // <-- class declaration goes here extern someclass someobj;  // someclass.cpp someclass someobj; 

and add vector inside constructor:

someclass(){    myvector.push_back(*this); } 

note myvector needs visible in scope.

your myvector should populated after include someclass.h file 1 instance of someclass. note if populating done in base class, don't have in every subsequent child class, because base constructor gets called anyhow.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -