gpt4 book ai didi

c++ - 如何在不影响其他兄弟类的情况下向需要它的继承类添加成员函数,同时保持多态性?

转载 作者:行者123 更新时间:2023-11-28 04:20:30 24 4
gpt4 key购买 nike

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

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);
};

STRING.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(或 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;
};

我们就到此为止吧。这就是了解不仅仅是接口(interface)变得重要的地方。对于多态性如何工作,控制流与类定义一样重要。只有一个抽象函数——进程。那是因为为了简单起见,假设那里有字符串模式扫描器,可以识别标记,对它们进行分类并生成看起来像是对象的链接列表,所有这些对象都基于标记,但每个对象都是一个实例具体类。完成此列表后,我们将遍历它,对每个列表调用 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.

现在这是您的运算符(operator)类 -

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”方法的代码不知道 token 的类型,但“process”的每个覆盖实现都知道,并且可以在其操作中使用特定于类的信息。希望对您有所帮助。

关于c++ - 如何在不影响其他兄弟类的情况下向需要它的继承类添加成员函数,同时保持多态性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com