Accessing a class stored in a vector, factory pattern

I have been trying to get a working factory pattern up and running, eventually for a simple strategy game. I think I now have the creation part working.

I can create new objects and store then in a vector (is this the "correct" way to store them?). However, I don't see how to access an object once it is stored. If I had an object with a function that say, made its attached sprite rotate, how would I call this function? How would I know which object is which?

Any tips or pointers will be greatly appreciated.

Cheers
Without knowing anything else about what you are doing, I would say that what you need is a base class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Sprite
{
public:
    virtual void TurnLeft(void) = 0;
    virtual void TurnRight(void) = 0;
    virtual void Move(int direction, int steps) = 0;
}

class Human : public Sprite
{
    //At this point you need to give TurnLeft(), TurnRight(), and Move() a definition.
    void TurnLeft(void)
    {
        //Assuming a console:
        cout << "I am a human turning left.";
    }
    //Do something similar with the other abstract functions.
    ...
}

...

//Your vector would be of type Sprite:
std::vector<Sprite> g_spritesCol;


By using the above technique (base class-derived classes), you don't need to know the specific implementation of each sprite. You just obtain the element with an iterator or something else and call for the appropriate function. A human will turn the way humans turn, while other characters will turn the way they are supposed to turn, etc.
Thanks for reply, however I already have various classes set up, and a vector which I can add new objects to from my factory class.

The bit that I don't understand is
just obtain the element with an iterator or something else and call for the appropriate function.

Once I have my object/s created and stored, how do I access them?
I'm no STL expert, but I believe you can access an individual item as you would with a regular array. Furthermore, you can use iterators to traverse the vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::vector<Sprite*> g_spritesCol;
...

void MakeThemAllMove(void)
{
    for (vector<Sprite*>::iterator it = g_spritesCol.begin(); it != g_spritesCol.end(); it++)
    {
        (*it)->TurnLeft();
    }
}

...
//Or accessing a specific one by index:
g_spritesCol[3]->TurnLeft();


You can read more about vectors here, as I'm no expert in the matter: http://www.cplusplus.com/reference/stl/vector/.
Thanks for that, I didn't expect it to be so simple.

Cheers
The same using STL
1
2
3
4
5
6
7
std::vector<Sprite*> g_spritesCol;
...

void MakeThemAllMove(void)
{
std::for_each(g_spritesCol.begin(), g_spritesCol.end(), std::mem_fun(&Sprite::TurnLeft));
}
If there is some kind of identifier to look up specific elements, you could store them in an associative container rather than a vector. This would make look ups of a specific one more efficient--logarithmic rather than linear.
Yeah, this is my first time attempting any STL stuff so I'm still learning what the different containers are.

Thanks! :)
Topic archived. No new replies allowed.