我可以使用 C++ 模板类来区分对象类型吗?或者我应该使用什么?
例如。我有一个类Synonym
,它可以是Statement, Procedure, etc
例如类型。我有接受这些同义词并根据其类型评估它们的函数。所以我想如果我能做这样的事情会很好:
enum Types { Statement, Procedure, Variable, ... };
template <typename Types>
class Synonym { ... }
void evaluate(Synonym<Statement> s, Synonym<Variable> v) { do something }
^ so that I can do this ... instead of checking the type in function like:
void evaluate(Synonym s, Synonym v) {
assert(s.type == Statement);
assert(v.type == Variable);
// also would like to eliminate things like: (if possible)
switch(s.type) {
case XXX: doSomething ...
case YYY: doAnotherThing ...
}
}