[英]Operation overloading in composite design pattern in C++
Suppose I have this code all set up: 假设我已经设置了以下代码:
class Function
{
public:
virtual double eval(double x) const =0;
};
class Polynomial : public Function
{
private:
std::vector<double> coefficients;
public:
// ...
};
class CompositeFunction : public Function
{
private:
char operation;
Function* left;
Function* right;
public:
// ...
};
CompositeFunction operator+(Function& f, Function& g) {
return CompositeFunction('+',&f,&g);
}
Now, I'm trying to do the following thing: 现在,我正在尝试执行以下操作:
CompositeFunction f = Polynomial({1,2}) + Polynomial({3,2});
printf("%lf\n",f.eval(1));
I don't get any compilation errors but when I try to eval f, Valgrind tells me I'm acessing bad data. 我没有任何编译错误,但是当我尝试评估f时,Valgrind告诉我我正在访问错误的数据。 I always get the correct answer but this is bugging me off. 我总是得到正确的答案,但这使我烦恼。 I have tried to stop using stack-allocated arguments but I can't overload any pointer operation. 我试图停止使用堆栈分配的参数,但无法重载任何指针操作。 Is there any pointer-less way or friendly for the users of these classes? 对于这些类的用户,有没有无指针的方法或友好的方法?
It's because f has references to two temporary objects. 这是因为f引用了两个临时对象。
Expand it out to make it more obvious: 扩展它使其更明显:
CompositeFunction f = operator+( Polynomial({1,2}), Polynomial({3,2}) );
f now holds references to the temporaries created by Polynomial({1,2}) and Polynomial({3,2}). f现在保留对由多项式({1,2})和多项式({3,2})创建的临时的引用。
You might want to consider using std::function<double(double)>
objects and lambdas, something like this: 您可能要考虑使用std::function<double(double)>
对象和lambda,如下所示:
#include <iostream>
#include <functional>
#include <vector>
typedef std::function<double(double)> Function;
Function polynomial(std::vector<double> const &coefficients) {
return [coefficients](double x) {
return x * coefficients[0]; // dummy evaluation
};
}
Function add(Function f1, Function f2) {
return [f1, f2](double x) { return f1(x) + f2(x); };
}
int main() {
Function f = add(polynomial({3,4}), polynomial({1,2}));
std::cout << f(3.3) << std::endl;
}
Here it is with std::shared_ptr: 这是与std :: shared_ptr在一起的:
#include <iostream>
#include <functional>
#include <memory>
#include <vector>
class Function
{
public:
virtual double eval(double x) const = 0;
virtual double derivative(double x) const = 0;
virtual ~Function() {}
};
typedef std::shared_ptr<Function> FunctionPtr;
class Polynomial : public Function
{
private:
std::vector<double> coefficients;
public:
// ...
Polynomial(std::vector<double> c) : coefficients(c) {}
};
class CompositeFunction : public Function
{
private:
char operation;
FunctionPtr left;
FunctionPtr right;
public:
// ...
CompositeFunction(FunctionPtr l, FunctionPtr r) : operation('+'), left(l), right(r) {}
};
FunctionPtr operator+(FunctionPtr f, FunctionPtr g) {
return std::make_shared<CompositeFunction>(f, g);
}
int main() {
auto p1 = std::make_shared<Polynomial>(std::vector<double>{1.0, 2.0});
auto p2 = std::make_shared<Polynomial>(std::vector<double>{3.0, 4.0});
auto f = std::make_shared<CompositeFunction>(p1, p2);
auto f2 = p1 + p2;
std::cout << f2->eval(3.3) << std::endl;
std::cout << f2->derivative(3.3) << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.