| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 
 | Literal *freeOpAddLeft(const Literal *left, const Literal *right) { return right->opAddRight(left); }
Literal *freeOpAddLeft(const Polynome *left, const Literal *right) { return right->opAddRight(left); }
 
Literal *freeOpAddRight(const Literal *left, const Literal *right) { return new Literal(...); }
Literal *freeOpAddRight(const Literal *left, const Polynome *right) { return new Literal(...); }
Literal *freeOpAddRight(const Polynome *left, const Literal *right) { return new Literal(...); }
Literal *freeOpAddRight(const Polynome *left, const Polynome *right) { return new Polynome(...); }
 
struct Literal {
    virtual ~Literal() { }
    virtual double evaluate(double x) const;
 
    virtual const Literal *opAddLeft(const Literal *right) const;
 
    virtual const Literal *opAddRight(const Literal *left) const;
};
 
struct Polynome : Literal {
    virtual ~Polynome() { }
    virtual double evaluate(double x) const { ... }
 
    virtual const Polynome *opAddLeft(const Literal *right) const { return freeOpAddLeft(this, right); }
 
    virtual const Polynome *opAddRight(const Literal *left) const { return freeOpAddRight(left, this); }
    virtual const Polynome *opAddRight(const Polynome *left) const { return freeOpAddRight(left, this); }
 
};
 
struct Plus : DoubleExprAndOperator {
    virtual ~Plus() { }
    Plus(const Literal *left, const Literal *right) : DoubleExprAndOperator(left, right) { }
    virtual Literal *evaluate() const { return left->opAddLeft(right); }
}; |