Array trying to be a vector
So I'm reading along, typing out code and just doing my thing, I then come across this baffling piece of code...now I was curious why the author didn't just use a vector b/c iterators make the word go round as far as I'm concerned (later on he did, so he won back my interest)...so rather he used this array system to remove the an observer object from a list of observers... now this is accomplished by the else if where if the address of the current iteration in the list is equal to the observer that called the function (I didnt print that part of the code, a this pointer is entered as RemoveObservers lone argument), then it is "removed" by simply overwriting that object with the next object in the list's address..what I'm not getting though is won't this leave a gap in your array? Here's the code:
Just a quick edit: m_Count is the size of the array of Observer objects called mp_List
Code:
void Subject::RemoveObserver(Observer* Item)
{
int i;
bool found = false;
for (i = 0; i < m_Count; i++)
{
if (found)
{}
else if (mp_List[i] == Item)
{
found = true;
mp_List[i] = mp_List[i+1];
}
}
if (found)
m_Count--;
}
If this does what I think it does and you have 5 objects in the array, and you are trying to remove the 3rd object, won't that end up leaving you with the 3rd and fourth being the same item? Sure, decrementing m_Count decreases the size of the array, but if you're not overwriting the second to last element, you're going to have duplicates...does this sound right? Any insight, any at ALL would be fantastic...this is driving me crazy hehe :eek: ->Chap