Is it possible to derive and use a C++ template class from a MFC class such as CDialog. I've tried, but the implementation falls over with the MFC macros used for message routing. For example;
template<class TYPE, class ARG_TYPE>
class CMyDialogT : public CDialog
{
public:
CMyDialogT(CMyContainerT<TYPE,ARG_TYPE> *pData,CWnd* pParent = NULL);
CMyContainerT<TYPE,ARG_TYPE> *m_pData;
// Generated message map functions
//{{AFX_MSG(CMyDialogT)
afx_msg void OnUpdateMyControl();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
template<class TYPE, class ARG_TYPE>
CMyDialogT<TYPE,ARG_TYPE>::CMyDialogT(CMyContainerT<TYPE,ARG_TYPE> *pData,CWnd* pParent)
: CDialog(CMyDialogT::IDD, pParent)
{
m_pData = pData;
}
BEGIN_MESSAGE_MAP(CGlobalEditT<TYPE,ARG_TYPE>, CDialog)
//{{AFX_MSG_MAP(CGlobalEditT)
ON_EN_UPDATE(IDC_MY_CONTROL, OnUpdateMyControl)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
The above fails to compile with a sequence of messages starting as follows;
warning C4002: too many actual parameters for macro 'BEGIN_MESSAGE_MAP'
error C2653: 'TYPE' : is not a class or namespace name
Is there any workaround for this, other than manually unrolling the MFC macros? I can't use template specialisation at this point, as given in a similar question here as I don't know all the possible values of TYPE and ARG_TYPE.
Another way of looking at the question would be 'can I embed a template class in another class without either specialising the template or making the host class a template class'. I can't answer this one either, I suspect the answer may be No.
Edit Partial solution for single type templates on MSDN here
You have to use BEGIN_TEMPLATE_MESSAGE_MAP instead of BEGIN_MESSAGE_MAP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With