I think what you may be after is the Abstract Factory pattern (rather than just the Factory Method pattern).
It's hard to see your intent without a greater context, so you may also want to check out the Singleton pattern. It provides two benefits: control of the number of instances of a class and it provides global access to the instance(s).
You could also use a static member in your class that holds a reference to the current objects. Typically, an STL map<UNIQUE_OBJ_ID, BaseClass *> would be used. That way a static getInstance( UNIQUE_OBJ_ID id ) method could look up the appropriate object.
How do you intend to use your class? Storing them in a vector means that either you intend to operate on all of the objects at once or you are storing an index (order of creation) somewhere else for later retrieval of an object.
From the snippet that you provided, I would guess that you need to create a base class, derive your classes from that, and store base class pointers/references in the container. You might also want to restrict the creation of your derived classes so that they must always be in your container of objects. The Singleton pattern with a static create method that returns a pointer/reference might be helpful.
I am writing an object manager that manages a base object which has derived objects that perform things differently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class baseObject {
public:
virtualvoid Step();
};
class obj_Ball: public baseObject {
public:
void Step() {
//Perform ball movement
}
};
class obj_test: public baseObject {
void Step() {
//Increase a variable by one and cout it.
}
The object manager would scroll through each instance in the instances list and perform their cooresponding Step method.
1 2 3
for ( int i=0; i < instances.size(); i++ ) {
instances[i].Step();
}
Also, upon the instance_create.... the instance would also perform their cooresponding constructors and deconstructors upon deletion.