C ++接口设计问题

时间:2014-02-20 16:29:25

标签: c++ inheritance interface polymorphism

我正在编写解析器,我有以下界面:

class IStatement
{
   // Represents instructions like "HelloWorld();"
   // Or flow control blocks like : "if( foo ) { bar(); }", "return 0;" etc...
public:
    virtual void execute( CScope & ) const = 0;
};

以下课程:

class CGoto : public IStatement // Basically a sequence of IStatement sub classes.
{
protected:
   vector<IStatement *>
public:
   virtual void execute( CScope & ) const; // Executes all statements.
};

class CConditional : public CGoto
{
protected:
   CExpression // Condition that must be true
public:
   virtual void execute( CScope & ) const; // If expression is true, executes all statements (i.e call CGoto::execute()).
};

我的问题是我想创建一个CIf类:

class CIf : public CConditional // Repesents a whole if/else if/ else block.
{
   // "this" is the "if" part of the if/else if/else block

   vector<CConditional *> _apoElseIfs; // "else if" parts, if any.

   CConditional * _poElse; // NULL if no "else" in if/else if/else block.

public:

   virtual void execute( CScope & roScope ) const
   {
      // HERE is my problem !
      // If the condition in the "if" part is true, i'm not going to execute
      // the else if's or the else.
      // The problem is that i have no idea from here if i should return because the
      // if was executed, or if i should continue to the else if's and the else.

      CConditional::execute( roScope );

      // Was the condition of the "if" true ? (i.e return at this point)

      // For each else if
      {
         current else if -> execute( roScope );
         // Was the condition of the current "else if" true ? (i.e return at this point)
      }

      else -> execute( roScope );
   }
};

我不知道,在我执行“if”或“else if”之后,如果我应该继续或返回。

我认为我可以使用布尔值作为方法execute()的返回值,该方法指示语句是否已被执行,但这对于非条件的IStatement实现没有意义。

我也可以这样做,以便CConditional类不测试条件本身,所以CConditional :: execute()执行语句而不管条件如何,并且无论操作类本身做什么,但是我想在CConditional :: execute()方法中包含该测试。

我希望我尽可能清楚地解释我的问题。你知道我怎么能干净利落地做到这一点吗?

谢谢:)

1 个答案:

答案 0 :(得分:2)

你的设计似乎有点混乱。

我会创建一个 Block 类,代表{ sttmnt1sttmnt2sttmntN }

class Block : public IStatement
{
    std::vector<IStatement*> statements;
public:
    virtual void execute( CScope & ) const { /* executes list of statements */ }
};

这样,你总是可以使用单个语句,并且可以使用Block类来处理多个语句。

还可以评估语句的 Expression 类,例如2 < x

class IExpression : public IStatement
{
public:
    virtual Value evaluate(CScope &scope) const = 0;
    virtual void execute( CScope &scope ) const { evaluate(scope); }
}

您需要一个Value类来表示表达式的结果。

最后, If 类wold具有一个 Expression 属性,一个 Statement 用于if部分和另一个(可选) )else部分。

class If: public IStatement
{
    IExpression *condition;
    IStatement *ifPart;
    IStatement *elsePart;

public:
    virtual void execute( CScope &scope ) const {
        if (condition->evaluate().asBoolValue()) {
            ifPart->execute(scope);
        }
        else if (elsePart) {
            elsePart->execute(scope);
        }
    }
}

要处理else if个案例,您只需将新If个对象设置为第一个else部分。