本文介绍了需要帮助switch()搞乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下情况(不是我的代码):


//假设已包含适当的标题


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 *);

};


类TypeB:public MyBaseClass {

typeB(const char *) ;

};


类TypeC:public MyBaseClass {

typeC(const char *);

};


//省略了类型*构造函数的实际实现


MyType MyBaseClass :: PickAClass(const char * str){

//使用str做秘密伏都教

返回ctypeA; //或ctypeB,或ctypeC,或ctypeNone

}


MyBaseClass * MyBaseClass :: Create(const char * str){

switch(PickAClass(s​​tr)){

case ctypeA:return(new TypeA(str));

case ctypeB:return(new TypeB(str));

case ctypeC:return(new TypeC(str));

default:return NULL;

}

}


(这里只有3种类型 - 实际代码中有20种。)


现在,这个设置正常,但它让我感到震惊,因为它是非常不理想的。我想创建一个函数数组

指向Type *构造函数的指针,所以我可以直接索引到

数组,而不是弄乱21个案例切换声明。我不会
假设我可以获取构造函数的地址,对吧?如果是这样,

是什么类型的?如果没有,如果实际上值得努力改造这个代码,你有什么建议吗?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

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.

推荐答案




你看过工厂模式吗?



Have you looked at the factory pattern?





如果你告诉我它在哪里我会的:)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



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.





你会指出不是构造函数,而是一个静态函数返回指向新分配实例的指针。



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


这篇关于需要帮助switch()搞乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 17:04