数学:模板专业= 0其他类
我解释我的问题。我想用模板一般事物编码数学。到目前为止,我上了两个课。我有一个,
template<typename T> class rational{
T numerator;
T denominator;
operators and constructors
}
并且我有一个
template<typename T> class polynomial{
vector<T> coefficients;
int degree;
operators and constuctors
}
,以便我可以使用Rational&lt; polyenmial&lt; int&lt; int&gt;&gt;&gt;
。到目前为止,这起作用了。但是我想在那些班级上添加一个衍生物。我想在class&lt; t&gt;
上使用它的定义也定义它。可以做到。但是,如何在int,double,on ractional&lt; int&gt;
等上使此派生型为= 0?这就像一个专业化,但是对于我没有指定的每种类型...谢谢!之后,我尝试将矩阵的事物模板模板...
目标是使用模板尽可能远。 接下来,我想将几个变量用于多项式...这是一个很棒的Projet!
感谢您的帮助 /任何信息=)
我认为答案就像< / strong>:
template<typename T> T Derivative(T object){ // for any other object !
return T(0);}
template<typename T> polynomial<T> Derivative(polynomial<T> object)
return compute_of_derivative, using too Derivative over T too (for generality);}
template<typename T> rational<T> Derivative(rational<T> object) // kind P / Q
return compute_of_derivative, which is ( D(P)Q -Q D(P) / Q^2 );}
这有效吗? : - /我会尽快尝试...
I explain my problem. I want to code math with template general thing. So far I've made two classes. I have a
template<typename T> class rational{
T numerator;
T denominator;
operators and constructors
}
And I have a
template<typename T> class polynomial{
vector<T> coefficients;
int degree;
operators and constuctors
}
these are made so that I can use rational<polynomial<rational<int>>>
. This works so far. But I want to add a Derivative on those class. I want to define it on the class<T>
using it's definition on T too. This can be made. But how can I make this Derivative be =0 on int, on double, on fractional<int>
, etc ? It's like a specialization, but for every type which I didn't specifie ... Thanks ! After this, I try to template the matrix thing ...
the goal is to go as far as possible with templates :-D
Next I want to use several variables for polynomial ... It's a great projet !
Thanks for any help / any information =)
I think answer would be like :
template<typename T> T Derivative(T object){ // for any other object !
return T(0);}
template<typename T> polynomial<T> Derivative(polynomial<T> object)
return compute_of_derivative, using too Derivative over T too (for generality);}
template<typename T> rational<T> Derivative(rational<T> object) // kind P / Q
return compute_of_derivative, which is ( D(P)Q -Q D(P) / Q^2 );}
is this working ? :-/ I will try it soon ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过超载衍生功能来做到这一点:
demo
You can do that by overloading the derivative function:
DEMO