Connecting Tech Pros Worldwide Help | Site Map

templated class

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 24th, 2007, 02:45 PM
Vincent RICHOMME
Guest
 
Posts: n/a
Default templated class

Hi,

I would need some help to write a templated function or method.
I am developping a GUI and on one plateform I am using a listbox
(CListBox) while on the other it's a combobox(CCombobox).
Both controls derived from a the same base class CWnd like this :

class CComboBox : public CWnd
{
....
int AddString(LPCTSTR lpszString);
}

class CListBox : public CWnd
{
....
int AddString(LPCTSTR lpszItem);
}


in my dialog I would like to write a method that take a CComboBox or a
CListBox and call the AddString method.



class CMyDialog : public CDialog
{
// take a CListBox or CCombobox
template<typename TList>
int AddListEntry(Tlist& List, LPCTSTR tszText, DWORD dwItemData);


void OnInitDialog()
{
AddListEntry(m_ListCtl, "eze", 0);
}



#ifdef PLATFORM1
CCombobox m_ListCtl;
#else
CListBox m_ListCtl;
#endif
};





How can I do that ?

  #2  
Old March 24th, 2007, 03:35 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
Default Re: templated class

On 2007-03-24 15:40, Vincent RICHOMME wrote:
Quote:
Hi,
>
I would need some help to write a templated function or method.
I am developping a GUI and on one plateform I am using a listbox
(CListBox) while on the other it's a combobox(CCombobox).
Both controls derived from a the same base class CWnd like this :
>
class CComboBox : public CWnd
{
...
int AddString(LPCTSTR lpszString);
}
>
class CListBox : public CWnd
{
...
int AddString(LPCTSTR lpszItem);
}
>
>
in my dialog I would like to write a method that take a CComboBox or a
CListBox and call the AddString method.
>
>
>
class CMyDialog : public CDialog
{
// take a CListBox or CCombobox
template<typename TList>
int AddListEntry(Tlist& List, LPCTSTR tszText, DWORD dwItemData);
>
>
void OnInitDialog()
{
AddListEntry(m_ListCtl, "eze", 0);
}
>
>
>
#ifdef PLATFORM1
CCombobox m_ListCtl;
#else
CListBox m_ListCtl;
#endif
};
You shouldn't need any macros to do this, (though I think you can use
macros and skip the templates). What you have above looks good to me,
just implement the AddListEntry() method and you should be good to go:

template<typename TList>
int CMyDialog::AddListEntry(TList& List, LPCTSTR tszText, DWORD dwItemData)
{
List.addString(tszText);
// ...
}

--
Erik Wikström
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.