0

I am writing a program that reads documents from a data base and turns the contents into C++ class instantiations. In each document there is a "type" field that I am mapping to a particular C++ class. Basically I read the document, construct an object of the corresponding class, and fill out the fields of the class object. I have maybe 10-15 different types of classes because the types of data in each document may be different, although some of the types are common. The common types of data are reflected in base/derived class definitions. Right now I have a switch statement with case statements corresponding to the different types of objects and in each case statement I create the right kind of object and then fill it in. It is a little messy though.

I was wondering if there is a means to place the various class definitions in an array so that I can mostly get rid of the switch statement. My ideal code would look like:

auto * obj = new arrayOfClassDefinitions[typeofClassIndex]

where arrayOfClassDefinitions contains the names of a set of classes. The obj pointer would then point to a new instantiation of the desired class.

I recall I could do things like this in Python but I am not sure about C++.

any ideas??

5
  • What do you mean by "place the various class definitions in an array"? A class definition is just some text. Commented Mar 15, 2017 at 17:39
  • 3
    The short answer is: C++ does not work this way. Commented Mar 15, 2017 at 17:41
  • Arrays will store the same data type for all elements. You could do an indirection by creating an array of pointers to the base class type and then point them to individual objects, but I'm not sure if that is what you are looking for. Commented Mar 15, 2017 at 17:52
  • What I had in mind was something was given a set of class definitions such as class1, class2, classN, I put them in an array and then I can create the desired class by just indexing into the array of class definitions and using the "new" operator. The array contains references to class definitions, not instantiations of a class. Commented Mar 15, 2017 at 17:53
  • In C++, classes are not objects. You can work around the issue by storing factory functions. Commented Mar 15, 2017 at 17:54

1 Answer 1

1

If you can organize all your classes into one hierarchical tree, you could use Abstract factory pattern to get similar result. This is example:

#include <map>
#include <memory>

class IBaseClass {
public:
    virtual ~IBaseClass(){}
};

class A : public IBaseClass {
public:
// ....
};

class B : public IBaseClass {
public:
// ....
};

class C : public IBaseClass {
public:
// ....
};

class IFactory {
    public:
    virtual ~IFactory(){}

    virtual IBaseClass* create() = 0;
};

template <class T>
class SimpleFactory :
    public IFactory {
public:
    IBaseClass* create() override {
        return new T();
    }
};

template <typename T>
std::shared_ptr<IFactory> make_factory() {
    return std::make_shared<SimpleFactory<T> >();

}

int main(int, char**)
{
    std::map<int, std::shared_ptr<IFactory>> factories = {
        {1, make_factory<A>()},
        {2, make_factory<B>()},
        {3, make_factory<C>()}
    };

    // ....

    int type_index = 1;
    auto obj = factories[type_index]->create();
    // ...
    // don't forget to delete
    delete obj;
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestions, it looks like factory functions will do what I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.