instance creating instances

It’s called a factory and it looks something like:

class Factory {
    Product create(int n);
    // ...
}

class Product {
    // ...
}

class Prod1 : public Product {
    // ...
}

int main() {
    Factory factory = Factory();
    Product prod[10] = factory.create(10);
    // ...

with create simply returning a Product object of some derived type. Of course, there’s usually some context passed into the Factory::create function to hint at the type of Product you want.

Leave a Comment