1
General / The 'new' keyword, factory patterns
« on: January 25, 2010, 07:57:01 pm »Quote from: "Walker"
Okay, I think the only bit now that I am unsure of is what sort of container to use to store objects and perhaps the actual create object function in the factory. Does the function return the object?Code: [Select]return new Pizza(X, Y);
orCode: [Select]object = new Pizza(X, Y);
return object;
What is the correct way to use the 'new' keyword?
EDIT: Okay, I managed to create a new object using a function, and add it to an STL vector, now my question is how do I access one of my stored objects?
Code: [Select]
vector <object> objectVec;
vector <object *> objectPtrVec;
// if each of your pushed back objects have an update function you would access the first object like this
int i = 0;
// if it is an object
objectVec[i].update();
// if it is a pointer to an object
objectPtrVec[i]->update();