Trying to reference a vector of sprites

Hello,
the title of the post is not very to explicit so I’ll to be precise.I have the following code

// in gameGlobals.h
struct GameGlobals
{
public:
    static Vector<cocos2d::Sprite*> enemies;
}

// in gameGlobals.cpp
Vector<Sprite*> GameGlobals::enemies;

Basically I have a vector of pointers to store the enemies that appear in game.
I have a class called Enemy that push to that vector the sprite when an enemy is created. I printed out the size of the vector after the push to check that it increases. So far so good.

Now I want to use that vector (Vector<Sprite*> GameGlobals::enemies;), in the constructor of another class. So if somewhere it has been modifyied, say that a bullet has deleted an enemy from the vector,
the class will know. So I passed a vector paramater to the constructor:

// someClass.cpp
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
    _enemies = enemies;  // _enemies being a private variable in the header   
                         // cocos2d::Vector<cocos2d::Sprite*> _enemies ;
}

// and I have this method too
void SomeClass::checkSizes()
{
        CCLOG("%i <", _enemies.size());
		CCLOG("%i <<<<", GameGlobals::enemies.size());
}

okey, so now in the helloWorld file I test it. So I initiallize the class in the init. The init also has a scheduler that creates enemies every 5 seconds. So then in the update method of HelloWorld i run SomeClass::checkSizes() and what I expect is that both sizes _enemies.size() and GameGlobals::enemies.size() are the same since I am passing that same parameter. This is the code

bool HelloWorld::init()
{
    //...        
    SomeClass::SomeClass(GameGlobals::enemies);
   // ... then I have the scedule create enemy instance
}

void HellowWorld::update(float dt)
{
    SomeClass::checkSizes();
}

But something when wrong because while the size of GameGlobals::enemies keeps increasing as expected, the result is that the size of _enemies is always zero.
Why is that? Since I did this

bool HelloWorld::init()
{
    SomeClass::SomeClass(GameGlobals::enemies)
}

and someClass.cpp I defined it as

SomeClass::SomeClass(Vector<Sprite*> enemies)
{
    _enemies = enemies; 
}

why is it not the same? I am sure it is some silly mistake but I can’t see it.
Thanks,
R

okey, hvind done a bit of research apparently it is a more complex topic because it is about removing an element from a vector while iterating through it. I’ll post a solution if I find it in case it help other.

You’re passing the vector by value, instead of by reference, which means it’s making a new copy of it. So, any changes to the vector in SomeClass affects its local copy, not the global object.

Change it to this to see if it helps:

SomeClass::SomeClass(Vector<Sprite*>& enemies)
{
    _enemies = enemies; 
}

// where _enemies is
Vector<Sprite*>& _enemies;

Thanks for your reply R101.
I did try that before posting but It was crashing and I though that I was doing something wrong. Turn out the crash was coming from somewhere else, but you are right, I should was passed it reference :+1:.
Long story short, it is about iterating the vector pass by reference, and most importantly, being able to delete while looping as shown here.

https://stackoverflow.com/questions/9927163/erase-element-in-vector-while-iterating-the-same-vector

cheers,
R