dimanche 16 janvier 2022

Factory Object for different objects types

Suppose to have the following class structure:

template <typename T, typename U>
class Base
{
  public:
    virtual ~Base<T, U>(){};
    virtual U show() = 0;

  protected:
    T foo;
};

class DerivateA : public Base<int, int>
{
  public:
    DerivateA() { foo = 7; };
    virtual int show() override { return foo; };
};

class DerivateB : public Base<std::string, std::string>
{
  public:
    DerivateB() { foo = "Hello"; };
    virtual std::string show() override { return foo; };
};

Is there any way I can create a factory object with a Create() function that, based on an input value, returns a pointer to either DerivateA or DerivateB?

Aucun commentaire:

Enregistrer un commentaire