home | |||
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 |
5 - Looking Over The CallsThe constructor and destructor are very straightforward, setting our variables and cleaning up afterwards:paLabel::paLabel(U16 uid,const char *label,const FONT *font,int width) : CLabel(uid,"",font), m_width(-1), m_origText(0), m_dispText(0), m_font(font) { Width(width); SetLabel(label); } paLabel::~paLabel(void) { delete[] m_origText; }Notice we only have one array to delete in the destructor, m_origText. We actually allocate one character array twice the length of the original text string. Halfway into this string, we set the pointer m_dispText. The reason is to reduce the calls to new[]. While not good programming practice, on a small class like this it is a timesaving tradeoff that can be worth the extra management. SetLabel() shows this array allocation in action, setting up enough room for two full strings, and then setting the m_dispText pointer: void paLabel::SetLabel( const char *buff ) { unsigned int newLen=strlen(buff)+1; if ( NULL==m_origText || newLen>strlen(m_origText)) { m_origText=new char[newLen+newLen+8]; m_dispText=&m_origText[newLen]; } strcpy(m_origText,buff); ResizeText(); }Setting the width and the font are very similar, in that the calls make their adjustments and then finish up by calling ResizeText(): void paLabel::Width(int width) { if (width!=m_width) // changed width? { m_width=width; ResizeText(); } } void paLabel::SetFont(const FONT *font) { m_font=font; CLabel::SetFont(m_font); // change base class font as well ResizeText(); } Previous Section Next Section |
||
Copyright © 2001-2006 ebmDevMag.com - Legal Notice |