jeudi 29 août 2019

Access factory which return baseClass* , from the baseClass Function itself

I have a scenario where I need to call a factory which returns baseClass * from a function of baseClass itself

I have a function() which is common for all the child class. So I decided to define that function in base class, and the derived class then can inherit from it. Now in that function we need to call a factory which returns the BaseClass*.

class BaseClass{
public:
virtual bool someFunc(){
  baseClass *var = Factory::GetObject(1);
  // do some processing on var
}
};

class ChildClass1: public BaseClass{
 ... some other functions
};

class Factory{  //factory
  public:
    static BaseClass * GetObject(int iter){
     if(iter==1){
      return ChildClass1;
     }
     else if(iter==2){
      return ChildClass1;
     }
     ....
    }
};

Understanding what would be the best design. Thanks in advance :)

Aucun commentaire:

Enregistrer un commentaire