Need help with switch() mess

Discussion in 'C++' started by Christopher Benson-Manica, Feb 6, 2004.

  1. 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.
    Christopher Benson-Manica, Feb 6, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Sharad Kala Guest

    "Christopher Benson-Manica" <> wrote in message
    news:c006cp$pjt$...
    > 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?
    Sharad Kala, Feb 6, 2004
    #2
    1. Advertising

  3. Sharad Kala <> 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.
    Christopher Benson-Manica, Feb 6, 2004
    #3
  4. Christopher Benson-Manica

    David Harmon Guest

    On Fri, 6 Feb 2004 13:55:37 +0000 (UTC) in comp.lang.c++, Christopher Benson-Manica <> 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.
    David Harmon, Feb 6, 2004
    #4
  5. "Sharad Kala" <> wrote in message
    news:c0075i$11d0vo$-> > 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.
    Jeremy Cowles, Feb 6, 2004
    #5
  6. 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.
    Christopher Benson-Manica, Feb 6, 2004
    #6
  7. Christopher Benson-Manica

    David Harmon Guest

    On Fri, 6 Feb 2004 14:16:16 +0000 (UTC) in comp.lang.c++, Christopher
    Benson-Manica <> was alleged to have written:
    >Sharad Kala <> 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
    David Harmon, Feb 6, 2004
    #7
  8. Christopher Benson-Manica

    Sharad Kala Guest

    Sharad Kala, Feb 6, 2004
    #8
  9. Christopher Benson-Manica

    Rolf Magnus Guest

    Christopher Benson-Manica wrote:

    > Sharad Kala <> 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.
    Rolf Magnus, Feb 6, 2004
    #9
  10. David Harmon <> 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.
    Christopher Benson-Manica, Feb 6, 2004
    #10
  11. Christopher Benson-Manica

    Chris Theis Guest

    Chris Theis, Feb 6, 2004
    #11
  12. 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
    Thomas Matthews, Feb 6, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cmtibmFpcg==?=

    Authentication mess

    =?Utf-8?B?cmtibmFpcg==?=, Jan 20, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    311
    =?Utf-8?B?cmtibmFpcg==?=
    Jan 21, 2005
  2. C@rLoS

    TabIndex a mess!

    C@rLoS, Apr 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    C@rLoS
    Apr 9, 2005
  3. Jake

    Help Turn IfThen mess into loop!

    Jake, Jan 28, 2005, in forum: ASP General
    Replies:
    2
    Views:
    120
  4. Becca Girl
    Replies:
    14
    Views:
    176
    Becca Girl
    Aug 4, 2008
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    106
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page