In C ++, can I represent a class type as a variable?

I would like to call a static method from a class that I will define at runtime, but which I know subclasses of this class. So let's say I have these classes

class super {
    public:
    super();
    static super *loadMe (ifstream &is);
}

class subA : public super {
   public:
   subA();
   static super *loadMe (ifstream &is);
}

class subB : public super {
   public:
   static super *loadMe (ifstream &is);
   private:
   subB();
}

And let me say what I want to determine at runtime (depending on what is in the file) whether to load subA or subB further. One way to do this is to use an empty object to call a method

super getLoadType (ifstream &is) { if(complicatedFunctionOfIs(is)) return subA(); return subB()}

super *newObj = getLoadType(is).loadMe(is);

but I created a constructor without subB private arguments, so I cannot do this. But in fact, I don’t need a super object, just a cool superclass class. So, is there a way to represent this as a variable?

EDIT: , , , .

+3
1

, Factory Pattern. , , factory. .

+9

Source: https://habr.com/ru/post/1771926/


All Articles