![]() |
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 ! |
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.