Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Any better way to do this virtual constructor? (http://www.velocityreviews.com/forums/t620906-any-better-way-to-do-this-virtual-constructor.html)

Nan Li 06-18-2008 04:37 AM

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 !


Daniel Pitts 06-18-2008 06:38 AM

Re: Any better way to do this virtual constructor?
 
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/>


All times are GMT. The time now is 06:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.