Re: templated class
On Sat, 24 Mar 2007 15:42:39 +0100, Vincent RICHOMME <richom.v@free.fr>
wrote:
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 ?
Which message are you talking about? Consider LB_ADDSTRING vs CB_ADDSTRING.
They have different values, but both CListBox and CComboBox wrap them with
a function AddString. If you determine the parameters and return codes are
identical for the two functions, you can simply call it from AddListEntry.
You don't even need AddListEntry in that case. To play it safer, and for
full generality, you can write some overloaded functions to do the work,
e.g.
int AddListEntry(CListBox& ctrl, LPCTSTR tszText, DWORD dwItemData);
int AddListEntry(CComboBox& ctrl, LPCTSTR tszText, DWORD dwItemData);
Just be sure their return codes mean the same thing. If the AddString
functions return different codes, you'll have to create your own return
codes to unify things so that callers don't have to worry about the type of
the control.
--
Doug Harrison
Visual C++ MVP