473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

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 981
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
3
by: uday | last post by:
can u explain me the concept of virtual constructor. Does such concept exists in c++
5
by: Devika | last post by:
hi, I understand the need of virtual destructor.but virtual constructor is not supported in c++.but basically why we need to have virtual constructor?? thx in advance Devika
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
by: Marcin Kalicinski | last post by:
Hi, Having the following program: #include <iostream> struct A { A() { std::cout << "A::A()\n"; } A(const A &) { std::cout << "A::A(const A &)\n"; }
7
by: Thomas Kowalski | last post by:
Hi everyone, is there a way to enforce that every class that inherit from BASE have to implement a given constructor? Example: class Base { virtual Base (int,int) = 0; } class INHERIT :...
1
by: Visame | last post by:
#include <iostream> using namespace std; class A { public: A() {cout << "constructor A" << endl;} A(A& a) {cout<<"copy A"<<endl;} virtual void Test(){cout <<"A test"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.