473,386 Members | 1,908 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,386 software developers and data experts.

Need help with switch() mess

At the moment, I've got the following situation (not my code):

// Assume appropriate headers have been included

enum MyType { ctypeNone, ctypeA, ctypeB, ctypeC };

class MyBaseClass {
static MyBaseClass* Create( const char * );
static MyType PickAClass( const char * );
};

class TypeA : public MyBaseClass {
typeA( const char * );
};

class TypeB : public MyBaseClass {
typeB( const char * );
};

class TypeC : public MyBaseClass {
typeC( const char * );
};

// actual implementation of Type* constructors omitted

MyType MyBaseClass::PickAClass( const char *str ) {
// do secret voodoo with str
return ctypeA; // or ctypeB, or ctypeC, or ctypeNone
}

MyBaseClass* MyBaseClass::Create( const char *str ) {
switch( PickAClass(str) ) {
case ctypeA: return( new TypeA(str) );
case ctypeB: return( new TypeB(str) );
case ctypeC: return( new TypeC(str) );
default: return NULL;
}
}

(Here there are only 3 types - in the actual code, there are 20.)

Now, this setup works correctly, but it strikes me as being
distinctively suboptimal. I'd like to create an array of function
pointers to the Type* constructors, so I could just index into that
array instead of messing with a 21-case switch statement. I don't
suppose I can take the address of a constructor, right? If so, what
is its type? If not, and if it is in fact worth the effort to revamp
this code, what do you suggest?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
11 1414

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
At the moment, I've got the following situation (not my code):

// Assume appropriate headers have been included

enum MyType { ctypeNone, ctypeA, ctypeB, ctypeC };

class MyBaseClass {
static MyBaseClass* Create( const char * );
static MyType PickAClass( const char * );
};

class TypeA : public MyBaseClass {
typeA( const char * );
};

class TypeB : public MyBaseClass {
typeB( const char * );
};

class TypeC : public MyBaseClass {
typeC( const char * );
};

// actual implementation of Type* constructors omitted

MyType MyBaseClass::PickAClass( const char *str ) {
// do secret voodoo with str
return ctypeA; // or ctypeB, or ctypeC, or ctypeNone
}

MyBaseClass* MyBaseClass::Create( const char *str ) {
switch( PickAClass(str) ) {
case ctypeA: return( new TypeA(str) );
case ctypeB: return( new TypeB(str) );
case ctypeC: return( new TypeC(str) );
default: return NULL;
}
}

(Here there are only 3 types - in the actual code, there are 20.)

Now, this setup works correctly, but it strikes me as being
distinctively suboptimal. I'd like to create an array of function
pointers to the Type* constructors, so I could just index into that
array instead of messing with a 21-case switch statement. I don't
suppose I can take the address of a constructor, right? If so, what
is its type? If not, and if it is in fact worth the effort to revamp
this code, what do you suggest?


Have you looked at the factory pattern?
Jul 22 '05 #2
Sharad Kala <no*****************@yahoo.com> spoke thus:
Have you looked at the factory pattern?


I will if you tell me where it is :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3
On Fri, 6 Feb 2004 13:55:37 +0000 (UTC) in comp.lang.c++, Christopher Benson-Manica <at***@nospam.cyberspace.org> was alleged to have written:
I'd like to create an array of function
pointers to the Type* constructors, so I could just index into that
array instead of messing with a 21-case switch statement. I don't
suppose I can take the address of a constructor, right?


You would point not to the constructor, but a static function that returns a pointer to a "new"ly allocated instance.

Jul 22 '05 #4
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c0*************@ID-221354.news.uni-> > MyBaseClass*
MyBaseClass::Create( const char *str ) {
switch( PickAClass(str) ) {
case ctypeA: return( new TypeA(str) );
case ctypeB: return( new TypeB(str) );
case ctypeC: return( new TypeC(str) );
default: return NULL;
}
}


Have you looked at the factory pattern?


The code he posted actually is a /crude/ factory.

Jul 22 '05 #5

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
Sharad Kala <no*****************@yahoo.com> spoke thus:
Have you looked at the factory pattern?


I will if you tell me where it is :)


oh..it's a design pattern.
I am referring to the GoF (gang of four) book "Design Patterns".
http://www.amazon.com/exec/obidos/tg...48-0909450?v=g
lance
Jul 22 '05 #6
On Fri, 6 Feb 2004 14:16:16 +0000 (UTC) in comp.lang.c++, Christopher
Benson-Manica <at***@nospam.cyberspace.org> was alleged to have written:
Sharad Kala <no*****************@yahoo.com> spoke thus:
Have you looked at the factory pattern?


I will if you tell me where it is :)


_Design Patterns - Elements of Reusable Object Orient Design_
Gamma, Helm, Johnson, and Vlissides (the Gang of Four)
Addison-Wesley 1995

Also http://www.google.com/search?q=factory+pattern

Jul 22 '05 #7
Christopher Benson-Manica wrote:
Sharad Kala <no*****************@yahoo.com> spoke thus:
Have you looked at the factory pattern?


I will if you tell me where it is :)


All over the world. One way to find it would be by using google.

Jul 22 '05 #8
Jeremy Cowles <jeremy.cowles[nosp@m]asifl.com> spoke thus:
The code he posted actually is a /crude/ factory.


"Crude" is right :) Is making a better one worth the effort?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #9
David Harmon <so****@netcom.com> spoke thus:
Also http://www.google.com/search?q=factory+pattern


I do appreciate that, although from the sound of it it was some
special library or implementation... UML isn't too useful ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #10

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
David Harmon <so****@netcom.com> spoke thus:
Also http://www.google.com/search?q=factory+pattern


I do appreciate that, although from the sound of it it was some
special library or implementation... UML isn't too useful ;)


Googling gives me tons of descriptions of the factory pattern (including
sample code). If you want to have a sophisticated solution you might try
http://www.cuj.com/documents/s=7994/cujcexp1906hyslop/

Cheers
Chris
Jul 22 '05 #11
Christopher Benson-Manica wrote:
At the moment, I've got the following situation (not my code):

// Assume appropriate headers have been included

enum MyType { ctypeNone, ctypeA, ctypeB, ctypeC };

class MyBaseClass {
static MyBaseClass* Create( const char * );
static MyType PickAClass( const char * );
};

class TypeA : public MyBaseClass {
typeA( const char * );
};

class TypeB : public MyBaseClass {
typeB( const char * );
};

class TypeC : public MyBaseClass {
typeC( const char * );
};

// actual implementation of Type* constructors omitted

MyType MyBaseClass::PickAClass( const char *str ) {
// do secret voodoo with str
return ctypeA; // or ctypeB, or ctypeC, or ctypeNone
}

MyBaseClass* MyBaseClass::Create( const char *str ) {
switch( PickAClass(str) ) {
case ctypeA: return( new TypeA(str) );
case ctypeB: return( new TypeB(str) );
case ctypeC: return( new TypeC(str) );
default: return NULL;
}
}

(Here there are only 3 types - in the actual code, there are 20.)

Now, this setup works correctly, but it strikes me as being
distinctively suboptimal. I'd like to create an array of function
pointers to the Type* constructors, so I could just index into that
array instead of messing with a 21-case switch statement. I don't
suppose I can take the address of a constructor, right? If so, what
is its type? If not, and if it is in fact worth the effort to revamp
this code, what do you suggest?


One method I use is to implement static creation methods
in each class. The method takes an ID parameter and will
return a pointer to a new instance if the ID is for the
class.

class TypeA
: public MyBaseClass
{
public:
static MyBaseClass * create(const char * id_text);
};

MyBaseClass *
TypeA ::
create(const char * id_text)
{
return (strcmp(id_text, "TypeA") == 0)
? new TypeA
: NULL;
}

In another method, called a factory, I call each creation
function. If the creation returns non-NULL, then I break
out of the loop. {If all creation functions return NULL,
then an exception.}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gerry | last post by:
As I'm not a PHP-prgrammer at all, I just need Help with this: I have had a guestbook-page in Europe and will now have to move it to a US based-server. This makes the time-function showing time...
8
by: Jimmy Smits | last post by:
Hi I have been playing with some JS that I cut from another page. It is a NCAA tournament bracket. When the user clicks on the submit button it sends the predictions to a db I have created. I got...
4
by: Guy | last post by:
I got a big Access file (1 400 tables) to convert to SQL and I would like to be provided with some automated tools, except upsizing wizard and DTS, to convert it on my own. I got a lot of forms...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
7
by: Andrew Tatum | last post by:
Alright, so I have this problem. I want to make it easy for me and others to be able to run a query and easily choose whether we want Merchants or NonMerchants. Previously, we would have to comment...
22
by: Nick Craig-Wood | last post by:
Did anyone write a contextmanager implementing a timeout for python2.5? I'd love to be able to write something like with timeout(5.0) as exceeded: some_long_running_stuff() if exceeded:...
10
by: Just Me | last post by:
I know you can stack case(s) case 1: case 2: but can you have ranges as in vb.net does like ( 1 to 6 ) type of thing, or <n or n etc.
0
by: Dale | last post by:
Access 2000 I have a check scanner from Magtek, (MicrImage) This is like a "point of sale" check scanner/credit card reader. Magtek furnished 2 ocx's (MTMicrImage.ocx & SaxComm8.ocx). They also...
4
by: junyang | last post by:
Hi all, I have one DTD fragment, base.dtd, that contains a bunch of useful element definitions (say an element named "base"), and two DTD fragments, a.dtd and b.dtd, that each build on base.dtd...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.