【发布时间】:2012-12-17 08:30:36
【问题描述】:
我遇到了设计问题:
对于某些命令,我有以下整数集:
addition = {2,3,4,5}
subtraction = {3,6,9}
add 的优先级是 > sub.
所以我创建了一个类
class Command{
int priority;
public:
bool operator <(const Command &com);
bool isInSet(int);
// i donot want this to be int ,
// may be tommorrow i go for strings or char.
// How to do this. AND
void execute(int); // should it be here?
};
因为这些命令对数据结构进行操作。
是否每个命令都知道如何执行自身,即命令本身是否应该有一个函数。
如果 execute() 进入命令内部,它将如何访问数据结构?所以 Command 也应该有一个指向这个 DS 的指针。
或者应该是一对,但类 Execution 需要指向 DS 的指针。
还是应该像 Command 向我发送一些枚举,然后我将其切换为类似
enum {PUSH, POP};
while(!commands[i].isInSet(3))
++i;
switch(comands[i].getName())
{
case PUSH:
// operations on DS i have all of them in this scope. :)
case POP:
...
}
我该怎么办?
还有什么好的方法吗? 谢谢。
【问题讨论】:
标签: c++ function pointers enums