Container of Constructors for Factory?

Hi All,
I'm writing a factory class, and was wondering if it is possible to get the address of a constructor, without having an object. I'd like to do something like follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef GenericClass (GenericClass::*pConstructor)(void)

Factory::Factory(void) {
    m_mapConstructors.insert(
      pair<string,pConstructor>
        (string("SpecificClass1"), &SpecificClass1::SpecificClass1);
    )
    m_mapConstructors.insert(
      pair<string,pConstructor>
        (string("SpecificClass2"), &SpecificClass2::SpecificClass2);
    )
}

GenericClass Factory::makeObject(string strName) {
    pConstructor pConstructorToUse;
    pConstructorToUse = m_mapConstructors[strName];
    return (*pConstructorToUse)();
}


The specific classes inherit from the Generic class. This sort of thing is no problem with member functions...if you have an object, but is there a way to do this with a constructor for a class with no instantiated object?

Basically, I'd like to get away from a long if/else clause in "makeObject".
Constructors are not "real" functions, and therefore the C++ standard explicitly disallows you from trying to take the address of a constructor either with or without an instance.


No, but you can use the Prototype pattern and clone instances of a created object.
Topic archived. No new replies allowed.