首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++接口设计问题

C++接口设计问题
EN

Stack Overflow用户
提问于 2014-02-20 16:29:24
回答 1查看 85关注 0票数 2

我正在编写解析器的代码,并具有以下接口:

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

以及下列课程:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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()方法中封装这个测试。

我希望我能尽可能清楚地解释我的问题。你知道我怎样才能干干净净的吗?

谢谢您:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-20 16:47:54

你的设计似乎有点混乱。

我会创建一个块类,表示一个{ sttmnt1sttmnt2sttmntN }

代码语言:javascript
复制
class Block : public IStatement
{
    std::vector<IStatement*> statements;
public:
    virtual void execute( CScope & ) const { /* executes list of statements */ }
};

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

也是可以计算的语句的表达式类,如2 < x

代码语言:javascript
复制
class IExpression : public IStatement
{
public:
    virtual Value evaluate(CScope &scope) const = 0;
    virtual void execute( CScope &scope ) const { evaluate(scope); }
}

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

最后,If类将一个表达式作为属性,一个语句用于if部件,另一个语句用于else部件(可选)。

代码语言:javascript
复制
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部分。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21913830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档