ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


7 - Coordinating Objects

Creating a new object means adding itself to this list:

  cpaRadioButton::cpaRadioButton(int id,
                                 int width,
                                 int height,
                                 const char *caption,
                                 int groupId)
    : CRadioButton(id,width,height,caption),
      m_tag(-1),
      m_groupId(groupId)
  {
    // add entry to list - simply place at beginning,
    // and move curr. pointer to us
    m_nextObject =s_firstObject;
    s_firstObject=this;
  }
Creation is fast because we can add our object to the start of the list. Deletion is slower, since we have to walk the list to find our object pointer first, and then splice the list to bypass ourselves:
  cpaRadioButton::~cpaRadioButton(void)
  {
    // remove itself from chain - walk along
    // list, looking for us, then swap out
    cpaRadioButton *ptr=s_firstObject;
    if ( this==ptr ) // we are first entry?
      s_firstObject=m_nextObject; // remove our link
    else // unlink from elsewhere
    {
      while ( NULL!=ptr )
      {
        if ( this == ptr->m_nextObject )
        {
          ptr->m_nextObject=m_nextObject;
          break;
        }
        ptr=ptr->m_nextObject;
      }
    }
  }
Walking a sequential list can appear extremely time-consuming, but before rewriting this code to use a more efficient algorithm, remember that there are not likely to be many cpaRadioButtons at any given time in your program, so you may be optimizing code that won't benefit significantly from it.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice