如何在保持多态性的同时,将成员函数添加到需要它的继承类中,而不会影响其他同级类?

How can I add a member function to the inherited class that needs it without affecting the other sibling classes all while maintaining Polymorphism?

本文关键字:继承 其他 影响 添加 多态性 函数 成员      更新时间:2023-10-16

我想将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);
};

这个问题需要一点工作。我们只有您的声明,如何以及何时应该称呼"get_left"和"get_right"很重要。看起来您正在为解析器编写一个标记化前端,因此作为旁注,我建议您查看yacc(或野牛或其变体之一)和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"的每个重写实现都知道,并且可以在其操作中使用特定于类的信息。希望有帮助。