470,849 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Any better way to do this virtual constructor?

Hello,
I have the following code now.

enum T1
{ A, B, C };

enum T2
{ D, E, F };
class Base
{
};

template < T1, T2 >
class Derived : public Base
{
} ;

Base * build( T1 t1, T2 t2 )
{
if ( t1 == A and t2 == D )
{
return new Derived<A,D>();
}
if ( t1 == A and t2 == E )
{
return new Derived<A,E>();
}
if ( t1 == A and t2 == F )
{
return new Derived<A,F>();
}
// ..... all the combinations here
}

I really hate to list all the T1-T2 combinations in the 'build'
functions. Is there any better way to code this? t1 and t2 are known
only at run time.

Thanks a lot !

Jun 27 '08 #1
1 913
Nan Li wrote:
Hello,
I have the following code now.

enum T1
{ A, B, C };

enum T2
{ D, E, F };
class Base
{
};

template < T1, T2 >
class Derived : public Base
{
} ;

Base * build( T1 t1, T2 t2 )
{
if ( t1 == A and t2 == D )
{
return new Derived<A,D>();
}
if ( t1 == A and t2 == E )
{
return new Derived<A,E>();
}
if ( t1 == A and t2 == F )
{
return new Derived<A,F>();
}
// ..... all the combinations here
}

I really hate to list all the T1-T2 combinations in the 'build'
functions. Is there any better way to code this? t1 and t2 are known
only at run time.

Thanks a lot !
Yes, don't make them template parameters, but member values instead. Is
there a reason that you need different types for this?

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stub | last post: by
13 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by ccs | last post: by
3 posts views Thread by uday | last post: by
5 posts views Thread by Devika | last post: by
26 posts views Thread by pmizzi | last post: by
16 posts views Thread by plmanikandan | last post: by
3 posts views Thread by Marcin Kalicinski | last post: by
7 posts views Thread by Thomas Kowalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.