c++ - 为数学函数 C++ 实现乘法运算符

标签 c++ operator-overloading

我有以下抽象基类:

class Function {
 virtual double Eval(double x) const = 0;
};     

我希望能够使用像 f * g 或 f->operator *(g) 这样的表达式,其中 f 和 g 是 Function 类的具体对象,在我的主文件中,例如当我想计算时一个定积分,这样我就可以写:

AnIntegrationMethod(f*g);

我想出的一个相当简单的方法包括声明一个 Product 类(仅显示头文件,实现很明显):

class Product: public Function {
 public: Product(Function *g, Function *f); 
  ~Product(); 
  virtual double Eval(double x) const; //return _g->Eval(x)*_f->Eval(x)
 private: Function *_g; Function *_f;
 };

然后在我的任何函数中

#include "Product.h"

class ConcreteFunction: public Function {
 public: 
   ConcreteFunction(); 
  ~ConcreteFunction(); 
  virtual double Eval(double x) const;
 Function* operator*(Function *f) {return new Product(this, f);}
 };

这实际上适用于简单的东西,但问题是运算符 * 仅在基类的单个派生类中定义,而不是为每个可能的派生类定义。这意味着,例如,如果我有一个表示数学函数的具体对象 f,我可以调用 f->operator *g 但如果我想再次调用 operator * 来获取对象 (f->operator * g)->operator * f 我做不到,因为函数 f* g 没有定义为 f 的 * 运算符。

我想我应该直接在我的基类中定义运算符 * 但后来我不知道如何实现该运算符,因为我真的不知道如何为产品获取正确的 Eval 函数,因为我无法使用现在类 Product,在类 Function 本身中使用从类 Function 派生的类 Product 是没有意义的。我想我也很困惑一般来说写如下内容是否正确:

 Function* Function::operator*(Function *f) {
 Function *g;
 ...
 //operations that allow g to be have the right Eval function
 //which should of the sort this->Eval(x) * f->Eval(x)
 ...
 return g;
 }

如有任何关于如何进行的提示或建议,我们将不胜感激。我的水平很基础,我已经编程两个月了。

最佳答案

只是一个草图,你可以这样做:

#include <memory>

// Base Function: f(x) = x
class Function
{
    protected:
    struct Implementation
    {
        virtual ~Implementation() {}
        virtual double evaluate(double x) const { return x; }
    };

    public:
    Function()
    :   self(std::make_shared<Implementation>())
    {}

    double operator () (double x) const { return self->evaluate(x); }

    protected:
    Function(std::shared_ptr<Implementation> self)
    :   self(self)
    {}

    private:
    std::shared_ptr<Implementation> self;
};
typedef Function Identity;


// Unary Function: u(-f(x))
class UnaryMinus : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Implementation(Function f)
        :   f(f)
        {};

        virtual double evaluate(double x) const override { return -f(x); }
    };

    public:
    UnaryMinus(Function f)
    :   Function(std::make_shared<Implementation>(f))
    {}
};

// Binary Function: u(f(x) + g(x))
class BinaryAdd : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Function g;
        Implementation(Function f, Function g)
        :   f(f), g(g)
        {};

        virtual double evaluate(double x) const override { return f(x) + g(x); }
    };

    public:
    BinaryAdd(Function f, Function g)
    :   Function(std::make_shared<Implementation>(f, g))
    {}
};

// Binary Function: u(f(x) * g(x))
class BinaryMultiply : public Function
{
    protected:
    struct Implementation : Function::Implementation
    {
        Function f;
        Function g;
        Implementation(Function f, Function g)
        :   f(f), g(g)
        {};

        virtual double evaluate(double x) const override { return f(x) * g(x); }
    };

    public:
    BinaryMultiply(Function f, Function g)
    :   Function(std::make_shared<Implementation>(f, g))
    {}
};

inline UnaryMinus operator - (Function f) { return UnaryMinus(f); }
inline BinaryAdd operator + (Function f, Function g) { return BinaryAdd(f, g); }
inline BinaryMultiply operator * (Function f, Function g) { return BinaryMultiply(f, g); }

#include <iostream>
int main() {
    Identity x;
    Function result = -x * (x + x) + x;
    std::cout << result(2) << '\n';
}

关于c++ - 为数学函数 C++ 实现乘法运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28681108/

相关文章:

c++ - CPU 周期计数 C++

c++ - 如何实现 Casts 实用程序命名空间

c++ - 给定两个点和两个 vector ,找到交点

c++ - 使用运算符重载添加两个数组对象会导致段错误

c++ - 没有typedef返回成员函数指针的语法是什么?

swift - swift 中运算符重载的规则不一致

java - "|="操作在 C++ 中是什么意思?

C++如何识别变量的类型

vb.net - VB.NET 中重载 'And' 运算符的示例是什么?

c++ - 使用 << 运算符同时写入文件和 cout