简体   繁体   English

从基类指针转换构造函数

[英]Conversion constructor from base class pointer

This is a question related to homework.这是一个与家庭作业有关的问题。 Basically I have to realize a scientific calculator.基本上我必须实现一个科学计算器。

Suppose that simplified code / hierarchy which represents a literal :假设表示文字的简化代码/层次结构:

struct Literal {
    virtual std::string toString() const = 0;
};
struct NumericLiteral : public Literal {};
struct IntegerLiteral : public NumericLiteral {
    int value;
    IntegerLiteral(int value) : value(value) {}
    std::string toString() const override { return std::to_string(value); }
};
struct RationalLiteral : public NumericLiteral {
    int num, den;
    RationalLiteral(int den, int num) : num(num), den(den) {}
    RationalLiteral(IntegerLiteral il) : num(il.value), den(1) {}
    std::string toString() const override { return std::to_string(num) + '/' + std::to_string(den); }
};
struct RealLiteral : public NumericLiteral {
    double value;
    RealLiteral(double value) : value(value) {}
    RealLiteral(IntegerLiteral il) : value(il.value) {}
    RealLiteral(RationalLiteral rl) : value(rl.num / (double)rl.den) {}
    std::string toString() const override { return std::to_string(value); }
};
struct ExpressionLiteral : public Literal {
    std::string expr;
    ExpressionLiteral() {}
    ExpressionLiteral(std::string expr) : expr(expr) {}
    ExpressionLiteral(IntegerLiteral nl) : expr(nl.toString()) {}
    ExpressionLiteral(RationalLiteral rl) : expr(rl.toString()) {}
    ExpressionLiteral(RealLiteral rl) : expr(rl.toString()) {}
    std::string toString() const override { return expr; }
};

As you can see, there is conversion constructor from the less general literals to the more general literals, eg Integer to Real.如您所见,存在从不太通用的文字到更通用的文字的转换构造函数,例如整数到实数。

At some point, I'll have to apply an operator of arity n on operands of type Literal * , and I need to get a vector of concrete literals based on the more general ( ExpressionLiteral > RealLiteral [...] > IntegerLiteral ).在某些时候,我将不得不在Literal *类型的操作数上应用一个n的运算符,并且我需要根据更一般的( ExpressionLiteral > RealLiteral [...] > IntegerLiteral )获得一个具体文字的向量。

So I tried something like this (example for ExpressionLiteral ) :所以我尝试了这样的事情(例如ExpressionLiteral ):

std::vector<ExpressionLiteral> v;
for (auto op : args) v.push_back(ExpressionLiteral(*op));

where args is std::vector<Literal*> .其中argsstd::vector<Literal*>

This was unsuccessful as ExpressionLiteral has no conversion constructor for Literal .这是不成功的,因为ExpressionLiteral没有Literal转换构造函数。

How can I call the conversion constructor corresponding to the real type of the Literal pointed ?如何调用与指向的Literal的实际类型相对应的转换构造函数?

Thanks for advance.感谢您的提前。

You need a way for a Literal to convert itself to an ExpressionLiteral , where that conversion is dependent on the runtype type of the Literal .您需要一种将Literal自身转换为ExpressionLiteral ,其中该转换取决于Literal的运行类型类型。 That's what virtual functions are for:这就是virtual函数的用途:

struct Literal {
    virtual ExpressionLiteral asExpression() const = 0;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM