I want to write something like

class Element{}; registerClass("element", &Element); class TextNode{} registerClass("text", &TextNode); 

and then do so

 TextNode text = crateInstance("text"); Element element = crateInstance("element"); 

Therefore, the registerClass function should save classes in the map How can I specify the type of this map?

 map<string, ???> classesMap; 

or C ++ is such a weak language that it is impossible to save a reference to a class in it?

  • It is not clear what you want to do. Pool of ready copies? Or just dynamically create instances of classes? - gbg
  • I want to make an array of references to classes, the phrase "classes" is different from the phrase "instances of classes" - Maxmaxmaximus
  • In C ++ classes of the first class? Can I save them somewhere? - Maxmaxmaximus
  • one
    in C ++, class declarations exist only until the program is compiled. Save class declaration somewhere is impossible. - gbg
  • one
    Well, you can store, for example, class factories - pointers to functions that will create instances of the desired class, with a cast to the desired type ... Ie it is possible to pervert, but is it necessary? - Harry

2 answers 2

Answer: in C ++ classes only the conditionality describes the data structures, and it disappears while the program is running, therefore it is not possible to refer to them. In other words, you cannot save a class somewhere, and then create instances for it. If you have such a need, most likely you are using techniques that you still have from javascript and other dynamic languages. Try to feel the spirit of C ++ and feel its style. And change the architecture of the program to remove the need for such class preservation. The developer should explicitly know where which class is used at the time of writing the program. This is the spirit of C ++.

    I will describe the code at the concept level:

     class instanciable { public: virtual ~instanciable() { } virtual instanciable *newInstance() = 0; } class Factory { map<string, instanciable *> registeredClasses; public: ~Factory() { // delete all registered classes // ... } instanciable *newInstance(string className) { if (registeredClasses->find(className) != registeredClasses.end()) { return registeredClasses[className]->newInstance(); } throw; } void register(string className, instanciable *newclass) { map[className] = newclass; } } class Class1 : public instanciable { public: // .... virtual instanciable *newInstance() { return new Class1(); } // .... } class Class2 : public instanciable { public: // .... virtual instanciable *newInstance() { return new Class2(); } // .... } void main() { Factory factory(); factory.register("Class1", new Class1()); factory.register("Class2", new Class2()); Class1 *cl1 = factory.newInstance("Class1"); Class2 *cl2 = factory.newInstance("Class2"); // .... cl1->method1(); // .... // delete cl1, cl2 } 

    Thus, we always keep in memory 1 object of a certain class, which is capable of creating itself. Classes of these objects implement a common interface so that we can save them in a map .