views:

265

answers:

1

Hello,

Is there any way to use IMPLEMENT_DYNCREATE with an template class? If not maybe someone could explain why?Or maybe there is another solution to this? :)

Example:

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

IMPLEMENT_DYNCREATE(A<CObject>, B);
+1  A: 

OK I've taken a quick look at the macros and thrown together a completely un tested macro that may 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);

Give it a try, as i say, it may work :D

Goz