Thread: Array trying to be a vector

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    Exclamation 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 ->Chap
    Last edited by Chaplin27; 03-02-2005 at 08:55 PM. Reason: extra info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM
  5. How can I convert a vector into an array??
    By shanedudddy2 in forum C++ Programming
    Replies: 6
    Last Post: 01-22-2003, 12:15 PM