IMPLEMENT_DYNAMIC using templates
I am wanting to use RegisterClass for a number of classes which inherit from CFormView. The classes are so similar (ie. they only differ by the dialog ID they use) that I would like to use a templated class and then call RegisterClass(CPageTemplate<IDD_THISDIALOG>); for each one. I am having problems with using IMPLEMENT_DYNAMIC in achieving this. The code for the template I am having problems with looks like this.
template<UINT iResource> class CPageTemplate : public CFormView
{
protected:
CPageTemplate<iResource>(){};
DECLARE_DYNCREATE(CPageTemplate)
// Form Data
public:
enum { IDD = iResource };
protected:
virtual ~CPageTemplate<iResource>(){};
};
template<UINT iResource>
IMPLEMENT_DYNAMIC(CPageTemplate<iResource>, CFormView)
Any help would be appreciated.
Re: IMPLEMENT_DYNAMIC using templates
You will need to pass the full template class name to DECLARE_DYNCREATE:
DECLARE_DYNCREATE(CPageTemplate<iResource>)
DECLARE_DYNCREATE provides the member declarations implemented by IMPLEMENT_DYNAMIC, so the class names passed to the two macros must match.
IMPLEMENT_DYNAMIC is a macro, not a function, so the template function syntax you used for it is not appropriate. Try just:
IMPLEMENT_DYNAMIC(CPageTemplate<iResource>, CFormView)
IMPLEMENT_DYNAMIC uses the class names passed to it to implement member data and a member function of that class. I don't know whether it will work correctly with a template class (I don't see why not), but if not, you can write your own template version based on the originals in afx.h (around line 827).
Dave