Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need help with switch() mess

Reply
Thread Tools

Need help with switch() mess

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-06-2004
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:ickAClass( 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.
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      02-06-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:c006cp$pjt$(E-Mail Removed)...
> 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:ickAClass( 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?


 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-06-2004
Sharad Kala <(E-Mail Removed)> 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.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      02-06-2004
On Fri, 6 Feb 2004 13:55:37 +0000 (UTC) in comp.lang.c++, Christopher Benson-Manica <(E-Mail Removed)> 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.

 
Reply With Quote
 
Jeremy Cowles
Guest
Posts: n/a
 
      02-06-2004
"Sharad Kala" <(E-Mail Removed)> wrote in message
news:c0075i$11d0vo$(E-Mail Removed)-> > 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.

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-06-2004
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.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      02-06-2004
On Fri, 6 Feb 2004 14:16:16 +0000 (UTC) in comp.lang.c++, Christopher
Benson-Manica <(E-Mail Removed)> was alleged to have written:
>Sharad Kala <(E-Mail Removed)> 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

 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      02-06-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:c007jg$q3p$(E-Mail Removed)...
> Sharad Kala <(E-Mail Removed)> 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-06-2004
Christopher Benson-Manica wrote:

> Sharad Kala <(E-Mail Removed)> 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.

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-06-2004
David Harmon <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RubyGems - update made a mess - help needed with Windows Becca Girl Ruby 14 08-04-2008 03:37 PM
Did ACDSee mess me up? Need some help baker1 Digital Photography 10 01-21-2006 04:16 PM
Help Turn IfThen mess into loop! Jake ASP General 2 01-28-2005 07:28 PM
PIX 515, DMZ, VPN, what a mess. Need Help. Eddie Cisco 9 06-20-2004 08:22 PM
need help not receiving any mess. lsmfav Computer Support 1 10-17-2003 02:51 PM



Advertisments