首页 > 解决方案 > 如何在保持多态性的同时将成员函数添加到需要它的继承类而不影响其他兄弟类?

问题描述

我想添加Token *get_left() const;class OPERATOR:public Token{}但为了多态性,我需要这样做:class Token {virtual Token *get_left() const = 0;}。这很好,但是因为class Token {}有其他继承的类,编译器迫使我对所有继承的类实现该函数。有没有办法只在需要它的类中拥有该函数,同时保持多态性?

令牌.h

class Token {
protected:
    Type type;
    Token *Next;
    Token(Type type);
public:
    Type get_type() const;
    void set_type(Type type);
    virtual char *get_value() const = 0;
    virtual int  get_len() const = 0;
    virtual Token *next() = 0;
    virtual void set_next(Token *tok_ptr) = 0;
    virtual Token *get_left() const = 0;
    virtual void set_left(Token *tok_ptr) = 0;
};

运算符.h

class OPERATOR:public Token {
private:
    char  *value;
    Token *left, *right;
    int   len;
public:
    OPERATOR(char *value);
    ~OPERATOR();
    char *get_value() const;
    void set_value(char *value);
    int get_len() const;
    void set_len(char *value);
    Token *get_left() const;
    void set_left(Token *tok_ptr);
    Token *get_right() const;
    void set_right(Token *tok_ptr);
    Token *next();
    void set_next(Token *tok_ptr);
};

字符串.h

class STRING: public Token {
private:
    int  len;
    char *value;
public:
    STRING(char *str);
    ~STRING();
    int get_len() const;
    void set_len(char *str);
    char *get_value() const;
    void set_value(char *str);
    Token *next();
    void set_next(Token *tok_ptr);
};

标签: c++

解决方案


这个问题需要一点工作。我们只有您的声明,如何以及何时调用“get_left”和“get_right”很重要。看来您正在为解析器编写标记化前端,因此作为旁注,我建议您查看 yacc(或 bison 或其变体之一)和 lex(或 flex 或其变体之一)。但是,让我们继续讨论 C++ 问题。如果只为特定子类讨论“get_left”和“get_right”是有意义的,那么作为一种技术/概念的多态性并不要求您在基类中的每个抽象级别都有您使用的每个方法。我将简化您的代码来说明:

enum Type
{
  string_type = 0,
  operator_type,
  ... // the rest
};

class token {
protected:  // stuff all tokens have (I guess)
  Type type; // side-note: if polymorphism is used correctly, a "type" field should not be needed
  std::string value; // they all seem to need value as well, so we put it here
  token *next; // all objects of type "token" need to be able to belong to linked list (let's say)

public:

  token(Type _type, const char* _value)
    : type(_type)
    , value(_value)
    , next(NULL)
  {}

  // if they share the above data, there's no real reason to make these
  // virtual, let alone abstract. If there REALLY is a different way
  // that a string returns its value as opposed to how an operator returns
  // it's value, then I guess you'd want to make a virtual function out
  // of the accessor, but you still may have a default implementation if
  // most tokens simply return the contents of their value string.

  Type get_type() const { return type; }
  void set_type(Type type) { this.type = type; }
  const char* get_value() const { return value.c_str(); }
  std::size_t get_len() const { return value.length(); }
  token* next() const { return next; }

  virtual void process() = 0;
};

让我们停在这里。这就是理解不仅仅是接口变得重要的地方。对于多态性如何工作,控制流与类定义一样重要。只有一个抽象函数——进程。这是因为为了简单起见,假设有字符串模式扫描器,可以识别标记,对它们进行分类并生成看起来像是对象的链表,所有这些都基于标记,但每个都是一个实例具体类。完成此列表后,我们将对其进行迭代,对每个列表调用 process 方法,该方法对每个列表进行适当的操作。这就是多态的本质。我不知道你的控制流实际上是什么,但如果它涉及不需要这些操作的对象上的 get_left 和 get_right ,你 已经“破坏了多态性”。所以你的扫描仪代码大致是 -

1. get next space delimited string
2. use contextual information to decide its type
   a. if type is an operator, create an object of type "operator" with type-specific data.
   b. same for string
   c. same for all other token types.
3. because each of these concrete objects are also instances of the base class
   token, you add each of them to a linked list with a head of type token*. The
   maintenance code for this list (adding, deleting, iterating) only has to
   know that these are all tokens.
4. repeat 1-3 until out of token candidates.
5. iterate through the list of abstract tokens, calling the "process" function.

所以现在这是你的操作员类 -

class operator_token : public token
{
private:
  // these are unique to "operator_token", but it has all the others from "token"
  token* left_operand;
  token* right_operand;

public:
  // the scanner code will be explicitly creating an instance of "operator_token"
  // with its unique constructor and the contextual information it needs,
  // but the code that adds it to the linked list and the code that iterates
  // through that linked list only knows it's a "token".

  operator_token(const char* value, token* left, token* right)
    : token(operator_type, value)
    , left_operand(left)
    , right_operand(right)
  {}

  virtual void process() override
  {
    // just something silly to illustrate. This implementation of "process"
    // can use left_operand and right_operand, because it will only be called
    // on an "operator_token" sub-class of "token".
    std::cout << "expression found: "
         << left_operand.get_value()
         << " " << value << " "
         << right_operand.get_value()
         << std::end;
  }
};

还有更简单的“string_token”子类——

class string_token : public token
{
// no private members because (in my simplified example) strings are pretty
// generic in their data needs
public:
  string_token(const char* value)
    : token(string_type, value)
  {}   // just the basics

  virtual void process() override
  {
     std::cout << "string: " << value << std::end;
  }
};

这就是多态性。调用“process”方法的代码不知道令牌的类型,但“process”的每个重写实现都知道,并且可以在其操作中使用特定于类的信息。希望有帮助。


推荐阅读