M
mathieu
Hi there,
I am looking for a trick to avoid maintaining the 'Create' function
as describe below. All it does is a simple template instantiation, are
there any trick which would avoid having to maintain this 'Create' as
the number of enum grows ?
Thanks
typedef enum {
TYPE1,
TYPE2
} TYPES;
class type {};
class type1 : public type {};
class type2 : public type {};
template <int T> struct Factory;
template <> struct Factory<TYPE1> { typedef type1 Type; };
template <> struct Factory<TYPE2> { typedef type2 Type; };
type* Create(TYPES e)
{
switch(e)
{
case TYPE1:
return new Factory<TYPE1>::Type;
break;
case TYPE2:
return new Factory<TYPE2>::Type;
break;
}
}
int main()
{
type *t1 = Create(TYPE1);
type *t2 = Create(TYPE2);
return 0;
}
I am looking for a trick to avoid maintaining the 'Create' function
as describe below. All it does is a simple template instantiation, are
there any trick which would avoid having to maintain this 'Create' as
the number of enum grows ?
Thanks
typedef enum {
TYPE1,
TYPE2
} TYPES;
class type {};
class type1 : public type {};
class type2 : public type {};
template <int T> struct Factory;
template <> struct Factory<TYPE1> { typedef type1 Type; };
template <> struct Factory<TYPE2> { typedef type2 Type; };
type* Create(TYPES e)
{
switch(e)
{
case TYPE1:
return new Factory<TYPE1>::Type;
break;
case TYPE2:
return new Factory<TYPE2>::Type;
break;
}
}
int main()
{
type *t1 = Create(TYPE1);
type *t2 = Create(TYPE2);
return 0;
}