0

I have templated class "Factory". The proto-type of factory is something as follows:

template <class T>
class Factory
{
    public:
    Factory();
    ~Factory();

    //few more functions & data-members


    private:
    //few more functions & data-members
};

In the main() I make multiple types of Factories as in --

int main()
{
    typedef Factory<int> IntFactory ;
    IntFactory A = IntFactory();

    typedef Factory<float> FloatFactory ;
    FloatFactory B = FloatFactory();

    //Complex is some user defined class
    typedef Factory<Complex> ComplexFactory ;
    ComplexFactory C = ComplexFactory();

    //Point3D is a user defined class
    typedef Factory<Point3D> Point3DFactory ;
    Point3DFactory D = Point3DFactory();

    //I may have several such initializations.

}

The question is how do I make an array of objects A,B,C,D incase I want to iterate through them?

2
  • 1
    You can't. Even if you make them all inherit from some common type FactoryBase, the best you could do would be an array of FactoryBase*. Commented Sep 28, 2012 at 13:49
  • You can use boost tuples to represent the array, and boost fusion to iterate over the elements. Commented Sep 28, 2012 at 13:50

3 Answers 3

1

The best I think you can do, (off the top of my head and not tested) is:

class FactoryBase
{
    public:
        virtual ~FactoryBase() = 0;
};

template <class T>
class Factory : public FactoryBase
{
    //...
};

int main()
{
    std::vector<FactoryBase*> factories;
    factories.push_back(new Factory<int>);
    factories.push_back(new Factory<Complex>);
    //... Make sure you delete these. Better yet, use a smart pointer.
    return 0;
}

Note a vector of pointers, and all that push_back(new ...) stuff is very vulnerable to exceptions and leaks...

Sign up to request clarification or add additional context in comments.

1 Comment

i dont want to use virtual methods. i m into parallel implementation of smething and virtual functions would kill vectorization. I want something on the lines as suggested by @Nim
1

(An alternative if you don't want to use the base class approach)

typedef boost::variant<IntFactory, FloatFactory, ...>  FactoryType;

std::vector<FactoryType> vFactories;

Then use a visitor to execute and methods...

Linking the documentation for variant.

7 Comments

can u please elaborate or give a reference. i do not understand sry..:(
also tell me in clarification to <IntFactory, FloatFactory, ...>, do I need to mention all the factories here? I do not know before hand what kind of factories r to be made
also can u tell, from this vector can I use typeid c++ keyword to compare types?
aaah, thtz wat i do not knw these. the thing is that the main code shall be outside the library and i wish that the user could make lot of factory and der wud be a class called Manager which wud hav array of all the factories. do u hav any other alternative?
I'm having a hard time groking your txt speak. Please write plain English. As I said, if you don't know the types beforehand, then your best bet is the base class approach. The overhead of a virtual function call will most likely be insiginificant compared to other operations. Write something simple that works, then profile...
|
0

There are many ways to do what you want to... each one has the pros and cons... In case you know all the instances during compiling time, this approach works fine:

Includes

#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/container/vector.hpp>

Functor

struct do_whatever
{
    template<typename T>
    void operator()(T& t) const
    {
        ...
    }
};

Instantiation and iteration over the vector

boost::fusion::vector<IntFactory, FloatFactory, ComplexFactory, Point3DFactory> vv(A, B, C, D);
boost::fusion::for_each(vv, do_whatever());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.