如何使每个派生类返回不同的int [英] How to make every derived class to return a different int

查看:52
本文介绍了如何使每个派生类返回不同的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正面临这个设计问题。


我有一个表定义一个对象的行为在给定的

状态下根据它接收的事件类型。


因此,对于每对情侣事件,状态我希望我在表中设置不同的

处理程序。


我的琐碎实现使用了定义(但我也可以使用

枚举)。


所以我有:

#define EVT_FOO 0

#define EVT_BAR 1

... 。

#define EVT_FOOBAR N


然后我为每个事件定义一个虚拟的GetType(),它返回

事件类型。


这是丑陋和尴尬的,因为每次我添加一个事件我都需要更新枚举/定义列表,而我希望能够

自动为每个派生类获得一个不同的整数

事件。


Mayeb我可以用某种方式得到这个结果运行时注册

机制。


我认为这个问题应该很常见,所以我想也许

有人可以建议一个比需要

枚举/定义列表更好的设计。


问候。

解决方案

Stefano Sabatini写道:


大家好,


我正面临这个设计问题。


我有一个表,根据它接收的事件类型定义给定

状态中Object的行为。


所以对于每对情侣,我希望我在表格中设置一个不同的

处理程序。


我的琐碎实现make使用定义(但我也可以使用

枚举)。


所以我有:

#define EVT_FOO 0

#define EVT_BAR 1

...

#define EVT_FOOBAR N


然后我为每个ev定义一个虚拟的GetType()返回

事件类型。


这是丑陋和尴尬的,因为每次我添加一个事件我都必须

更新枚举/定义列表,而我希望能够为每个派生类

事件自动获得一个不同的整数。 br />

Mayeb我可以通过某种运行时注册获得这个结果

机制。



这样的事情怎么样:

unsigned int

count(void){

static unsigned int c = 0;

return(c ++);

}


struct reg_base {


虚拟

unsigned int get_id(void)= 0;


};


模板< typename D>

class reg:public reg_base {


static unsigned int const id;


public:


unsigned int get_id(void){

return(id);

}


};


模板< typename D>

unsigned int const reg< D> :: id = count();

struct X:public reg< X {};

struct Y:public reg< Y {};


#include< iostream>


int main(void){

X x;

Y y1;

Y y2;

reg_base * p = new Y();


std :: cout<< X表示X。 << x.get_id()<< ''\ n'';

std :: cout<< Y << y1.get_id()<< ''\ n'';

std :: cout<< Y << y2.get_id()<< ''\ n'';

std :: cout<< Y << p-> get_id()<< ''\\ n';;

}


你还可以添加一个静态方法来产生一个类型的id。


我觉得这个问题应该很常见,所以我想也许

有人可以建议一个比需要更好的设计

enum /定义列表。



Best


Kai-Uwe Bux


< blockquote> On 2008-09-22 16:14,Stefano Sabatini写道:


大家好,


我'' m面对这个设计问题。


我有一个表,它根据收到的事件类型定义给定

状态中Object的行为。


因此,对于每对情侣活动,我希望我在表格中设置一个不同的

处理程序。


我的琐碎实现使用了定义(但我也可以使用

枚举)。


所以我有:

#define EVT_FOO 0

#define EVT_BAR 1

...

#define EVT_FOOBAR N


然后我为每个事件定义一个虚拟的GetType(),它返回

事件类型。


这是丑陋和尴尬的,因为我每次添加我必须要事件
更新枚举/定义列表,虽然我希望能够为每个派生类的

事件自动获得一个不同的整数。


Mayeb我可以通过某种运行时注册来获得这个结果

机制。



您可以在基类中放置一个const int,并在每个事件类的构造函数中将其设置为正确的

值。


我认为这个问题应该很常见,所以我想也许

有人可以建议一个比需要更好的设计/>
enum /定义列表。



看起来你的设计有问题。如果你想使用

数字来区分不同的事件并根据它执行

不同的动作也许你应该考虑重载一些功能

取而代之的是不同的事件类型。


-

Erik Wikstr ?? m


< blockquote class =post_quotes>
unsigned int

count(void){

* static unsigned int c = 0;

* return(c ++);


}


struct reg_base {


* virtual

* unsigned int get_id(void)= 0;


};


template< typename D>

class reg:public reg_base {


* static unsigned int const id;


public :


* unsigned int get_id(void){

* * return(id);

*}


};


模板< typename D>

unsigned int const reg< D> :: id = count();


struct X:public reg< X {};

struct Y:public reg< Y {};


#include< iostream>


int main(无效){

* X x;

* Y y1;

* Y y2;

* reg_base * p = new Y();


* std :: cout<< X表示X。 << x.get_id()<< ''\ n'';

* std :: cout<< Y << y1.get_id()<< ''\ n'';

* std :: cout<< Y << y2.get_id()<< ''\ n'';

* std :: cout<< Y << p-> get_id()<< ''\ n'';


}



我认为完全消除计数功能更好将

计数功能放入类/结构中,例如,在你的情况下,reg_base。

struct counter {


static unsigned int count getId(return id ++;)

private:

static unsigned int id;


};

unsigned int counter :: id = 0;

或者更清晰一些。我不喜欢代码中的无会员功能

。至少,如果我们要使用这个函数,我们应该命名它,因为它的使用时间短,而且比b $ b更容易编码。我刚才提到的另一种选择。


Hi all,

I''m facing this design problem.

I have a table which defines the behaviour of an Object in a given
state according to the type of event it is receiving.

So for each couple event,state I want I set in the table a different
handler.

My trivial implementation make use of defines (but I could have used
an enum as well).

So I have:
#define EVT_FOO 0
#define EVT_BAR 1
....
#define EVT_FOOBAR N

Then I define for each event a virtual GetType() which returns the
event type.

This is ugly and awkward, since everytime I add an event I have to
update the enum/list of defines, while I would like to be able to
automatically get a different integer for every derived class of
Event.

Mayeb I could get this result with some kind of run-time registration
mechanism.

I think this problem should be quite common, so I thought maybe
someone can suggest a better design than that which requires the
enum/defines list.

Regards.

解决方案

Stefano Sabatini wrote:

Hi all,

I''m facing this design problem.

I have a table which defines the behaviour of an Object in a given
state according to the type of event it is receiving.

So for each couple event,state I want I set in the table a different
handler.

My trivial implementation make use of defines (but I could have used
an enum as well).

So I have:
#define EVT_FOO 0
#define EVT_BAR 1
...
#define EVT_FOOBAR N

Then I define for each event a virtual GetType() which returns the
event type.

This is ugly and awkward, since everytime I add an event I have to
update the enum/list of defines, while I would like to be able to
automatically get a different integer for every derived class of
Event.

Mayeb I could get this result with some kind of run-time registration
mechanism.

What about something like this:
unsigned int
count ( void ) {
static unsigned int c = 0;
return ( c++ );
}

struct reg_base {

virtual
unsigned int get_id ( void ) = 0;

};

template < typename D >
class reg : public reg_base {

static unsigned int const id;

public:

unsigned int get_id ( void ) {
return ( id );
}

};

template < typename D >
unsigned int const reg<D>::id = count();
struct X : public reg<X{};
struct Y : public reg<Y{};

#include <iostream>

int main ( void ) {
X x;
Y y1;
Y y2;
reg_base * p = new Y ();

std::cout << "X " << x.get_id() << ''\n'';
std::cout << "Y " << y1.get_id() << ''\n'';
std::cout << "Y " << y2.get_id() << ''\n'';
std::cout << "Y " << p->get_id() << ''\n'';
}

You could also add a static method that yields the id of a type.

I think this problem should be quite common, so I thought maybe
someone can suggest a better design than that which requires the
enum/defines list.


Best

Kai-Uwe Bux


On 2008-09-22 16:14, Stefano Sabatini wrote:

Hi all,

I''m facing this design problem.

I have a table which defines the behaviour of an Object in a given
state according to the type of event it is receiving.

So for each couple event,state I want I set in the table a different
handler.

My trivial implementation make use of defines (but I could have used
an enum as well).

So I have:
#define EVT_FOO 0
#define EVT_BAR 1
...
#define EVT_FOOBAR N

Then I define for each event a virtual GetType() which returns the
event type.

This is ugly and awkward, since everytime I add an event I have to
update the enum/list of defines, while I would like to be able to
automatically get a different integer for every derived class of
Event.

Mayeb I could get this result with some kind of run-time registration
mechanism.

You could put a const int in the base-class and set it to the correct
value in the constructor of each event class.

I think this problem should be quite common, so I thought maybe
someone can suggest a better design than that which requires the
enum/defines list.

It seems like you have a problem with your design. If you want to use
the number to distinguish the different events and based on that perform
different actions perhaps you should consider overloading some function
for the different event types instead.

--
Erik Wikstr??m


unsigned int
count ( void ) {
* static unsigned int c = 0;
* return ( c++ );

}

struct reg_base {

* virtual
* unsigned int get_id ( void ) = 0;

};

template < typename D >
class reg : public reg_base {

* static unsigned int const id;

public:

* unsigned int get_id ( void ) {
* * return ( id );
* }

};

template < typename D >
unsigned int const reg<D>::id = count();

struct X : public reg<X{};
struct Y : public reg<Y{};

#include <iostream>

int main ( void ) {
* X x;
* Y y1;
* Y y2;
* reg_base * p = new Y ();

* std::cout << "X " << x.get_id() << ''\n'';
* std::cout << "Y " << y1.get_id() << ''\n'';
* std::cout << "Y " << y2.get_id() << ''\n'';
* std::cout << "Y " << p->get_id() << ''\n'';

}

I think it''s better to eliminate count function altogether and put the
count functionality into class/struct such as, in your case, reg_base.
struct counter{

static unsigned int count getId(return id++;)

private:
static unsigned int id;

};
unsigned int counter::id=0;
Or some cleaner variation of that. I don''t like member-less function
in the code. At the very least, we ought to namespace it if we''re to
have this function, given that it''s short to use and easier to code
than an alternative I''ve just mentioned.


这篇关于如何使每个派生类返回不同的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆