C++接口(interface)设计问题

标签 c++ inheritance interface polymorphism

我正在编写一个解析器,并且有以下接口(interface):

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):

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”之后我是否应该继续或返回。

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

我还可以使 CConditional 类不测试条件本身,并且使 CConditional::execute() 执行语句而不管条件如何,并且无论操纵该类本身,但我希望将该测试封装在 CConditional::execute() 方法中。

我希望我尽可能清楚地解释了我的问题。您知道我该如何干净地做到这一点吗?

谢谢:)

最佳答案

你的设计看起来有点困惑。

我会创建一个Block类,代表{ sttmnt1 , sttmnt2 , sttmntN }

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

这样,您始终可以使用 if 单个语句,并且可以使用 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 类将有一个表达式 作为属性,一个语句 用于 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第一个的一部分。

关于C++接口(interface)设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913830/

相关文章:

java - 枚举类如何扩展另一个外部库类?

c++ - 为什么头文件中定义的 C++ 虚函数可能无法在 vtable 中编译和链接?

c++ - 如何在 C++ 中将十六进制值的字符数组转换为单个整数值?

c++ - 来自第三类对象 C++ 的垃圾值

c++ - 使用boost range_iterator数组来分割字符串

java - 在Java中,如何让子类决定使用哪种类型参数类作为重写方法参数?

python - Python 3.6 (Spyder) 中的类和继承

java - Java中如何向下转换数组?

c++ - 制作几何库的良好设计方法(关于是否使用 union )?

c# - 我应该多积极地进行子类化以保持 DRY?