Template Base Class with Factories

Q

quelcher

Hi, I'm going to use a simplified example to ask my question.

Suppose I have a base class:

template <class T> class BaseClass {
.....
};

with a couple of classes which inherit it

class Example1 : public BaseClass<int> {
.....
};

class Example2 : public BaseClass<float> {
.....
};

Question: How do I write a Factory class to return a reference to
BaseClass? Such as:

class FactoryClass {

public:
template <class T> BaseClass<T> *Create(int option) {
switch (option) {
case 0 : return(new Example1);
case 1: return(new Example2);
};
}
};

Where in my code I would call it as such.....

FactoryClass thisFactory;
BaseClass<???> *thisBaseClass1 = thisFactory.Create(0);
BaseClass<???> *thisBaseClass2 = thisFactory.Create(1);

Now, I know that BaseClass is defined with a template parameter, but
in this case I do not know what that parameter is until the factory
creates it. Is there a way to define the local reference to BaseClass
in the code such that "I don't know what the type is and I don't care
at this point"? Go easy on me, I'm relatively new to templates.

Thanks!

D
 
V

Vidar Hasfjord

Hi, I'm going to use a simplified example to ask my question.

Suppose I have a base class:

template <class T> class BaseClass {
    .....

};

with a couple of classes which inherit it

class Example1 : public BaseClass<int> {
   .....

};

class Example2 : public BaseClass<float> {
  .....

};

Question:  How do I write a Factory class to return a reference to
BaseClass?  Such as:

class FactoryClass {

 public:
          template <class T> BaseClass<T> *Create(int option) {
                                 switch (option) {
                                     case  0 :  return(new Example1);
                                     case  1:   return(new Example2);
                                  };
             }

};

Where in my code I would call it as such.....

  FactoryClass  thisFactory;
  BaseClass<???> *thisBaseClass1 = thisFactory.Create(0);
  BaseClass<???> *thisBaseClass2 = thisFactory.Create(1);

Now, I know that BaseClass is defined with a template parameter, but
in this case I do not know what that parameter is until the factory
creates it.  Is there a way to define the local reference to BaseClass
in the code such that "I don't know what the type is and I don't care
at this point"?  Go easy on me, I'm relatively new to templates.

You lack a common base class. Despite its name BaseClass is a template
that generates unrelated base classes. One possible solution is to
derive the generated base classes from a common ancestor:

class GrandBase {/*...*/};

template <class T>
class BaseClass : public GrandBase {/*...*/};

//...

FactoryClass f;
GrandBase* a = f.Create (0);
GrandBase* b = f.Create (1);

Regards,
Vidar Hasfjord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top