MFC IMPLEMENT_DYNCREATE with template

Can it be used IMPLEMENT_DYNCREATE

with a template class? If not, why not? Is there another solution?

Example:

template<typename T>
class A : public B{
public:
A(){ printf("A constuctor "); }
void fn( ){ T* a = new T(); }
};

IMPLEMENT_DYNCREATE(A<CObject>, B);

      

+2


source to share


2 answers


OK I took a quick look at the macros and put together a completely untested macro that might work.

#define _RUNTIME_CLASS(class_name, template_name) ((CRuntimeClass*)(&class_name<template_name>::class##class_name##template_name))
#define RUNTIME_CLASS(class_name, template_name) _RUNTIME_CLASS(class_name, template_name)

#define _IMPLEMENT_RUNTIMECLASS( class_name, template_name, base_class_name, wSchema, pfnNew, class_init ) \
                                 AFX_COMDAT CRuntimeClass class_name<template_name>::class##class_name##template_name = { \
                                 #class_name, sizeof(class class_name<template_name>), wSchema, pfnNew, \
                                 RUNTIME_CLASS(base_class_name), NULL, class_init }; \
                                 CRuntimeClass* class_name<template_class::GetRuntimeClass() const \
                                 { return RUNTIME_CLASS(class_name, template_name); }

#define IMPLEMENT_DYNCREATE( class_name, template_name, base_class_name ) \
                             CObject* PASCAL class_name<template_name>::CreateObject() \
                             { return new class_name<template_name>; } \
                             IMPLEMENT_RUNTIMECLASS(class_name, template_name, base_class_name, 0xFFFF, \
                             class_name<template_name>::CreateObject, NULL)

      

Then you can call:



IMPLEMENT_DYNCREATE( A, CObject, B);

      

Try as I said, it might work: D

+2


source


Mostly Visual Studio 2005 supports the runtime class, but Microsoft has pushed macros out since Visual Studio 2010 (or 2008).



Solution: Copy the template class runtime macros from afx.h

visual studio 2005 and create your .h

file.

0


source







All Articles