用于派生 类 的通用对象,用于实例化和返回目的

Generic object for derived classes for instantiation and returning purposes

我想创建一个图形对象,当我沿着它绘制时,它可以在不同的数学函数之间切换。我目前将当前数学函数作为一个对象保留在图形对象中,因此当我调用它的绘图函数时它知道要遵循什么,并且我正在尝试使用多态性来描述不同类型的数学函数。我当前的(非工作)代码如下所示:

#pragma once
#include<array>

class MathExpression
{
public:
    virtual ~MathExpression() = default;
    virtual MathExpression integrate() const = 0;
};

class Polynomial : public MathExpression
{
public:
    Polynomial(std::array<double, 5> coefficients) : coefficients(coefficients) {};
    Polynomial() : coefficients(std::array<double, 5>{0.0, 0.0, 0.0, 0.0, 0.0}) {};
    Polynomial(const Polynomial& p) : coefficients(p.coefficients) {};
    ~Polynomial() = default;

    Polynomial integrate() const;
    std::array<double, 5> getCoefficients() const;
private:
    std::array<double, 5> coefficients;
};

class Exponential : public MathExpression
{
public:
    Exponential(std::array<double, 3> coefficients) : coefficients(coefficients) {};
    Exponential() : coefficients(std::array<double, 3>{0.0, 0.0, 0.0}) {};
    Exponential(const Exponential& e) : coefficients(e.coefficients) {};
    ~Exponential() = default;

    Exponential integrate() const;
    std::array<double, 3> getCoefficients() const;
private:
    const std::array<double, 3> coefficients;
};

class Graph
{
public:
    Graph() = default;
    MathExpression getCurrentCurve() {return current_curve;}
    void setCurrentCurve(const MathExpression& curve) {
        current_curve = curve;
        return;
    }
private:
    MathExpression current_curve;
};

此代码不起作用,因为 mathExpression 是一个包含纯虚函数的抽象 class,因此我既不能创建它的实例,也不能从函数中创建它 return。我也从派生的 classes 的 integrate() 函数中得到错误,即 return 类型 (Polynomial/Exponential) 与 return 类型 MathExpression 不相同也不协变覆盖的 integrate() 函数。

我需要找到解决这两个问题的方法:

我该如何解决this/work这些问题?

正如@PaulMcKenzie 所建议的,只需使用多态性(例如,使用智能指针)。也就是说,您管理指向基 class、MathExpression 的指针,并创建派生的 classes、PolynomialExponential 的堆实例(使用 newmake_uniquemake_shared)。由于您似乎要返回派生 classes 的实例并共享它们,您可能需要使用 shared_pointers.

开始的可能实现:

[Demo]

#include <array>
#include <memory>  // make_shared, shared_ptr

class MathExpression
{
public:
    virtual ~MathExpression() = default;
    virtual std::shared_ptr<MathExpression> integrate() const = 0;
};

class Polynomial : public MathExpression
{
public:
    Polynomial(std::array<double, 5> coefficients) : coefficients(coefficients) {};
    Polynomial() : coefficients(std::array<double, 5>{0.0, 0.0, 0.0, 0.0, 0.0}) {};
    Polynomial(const Polynomial& p) : coefficients(p.coefficients) {};
    ~Polynomial() = default;

    std::shared_ptr<MathExpression> integrate() const { return std::make_shared<Polynomial>(); }
    std::array<double, 5> getCoefficients() const;
private:
    std::array<double, 5> coefficients;
};

class Exponential : public MathExpression
{
public:
    Exponential(std::array<double, 3> coefficients) : coefficients(coefficients) {};
    Exponential() : coefficients(std::array<double, 3>{0.0, 0.0, 0.0}) {};
    Exponential(const Exponential& e) : coefficients(e.coefficients) {};
    ~Exponential() = default;

    std::shared_ptr<MathExpression> integrate() const { return std::make_shared<Exponential>(); }
    std::array<double, 3> getCoefficients() const;
private:
    const std::array<double, 3> coefficients;
};

class Graph
{
public:
    Graph() = default;
    std::shared_ptr<MathExpression> getCurrentCurve() { return current_curve; }
    void setCurrentCurve(const std::shared_ptr<MathExpression>& curve) {
        current_curve = curve;
        return;
    }
private:
    std::shared_ptr<MathExpression> current_curve{};
};

int main() {
    std::shared_ptr<MathExpression> p{std::make_shared<Polynomial>()};
    std::shared_ptr<MathExpression> e{std::make_shared<Exponential>()};
}