I have a question regarding “this” keyword. I have a class called “BiPoly”, which represents bivariate polynomial. There is a member function called BiPoly<NT>::DifferentiateX(), which gets Partial Differentiation wrt X, and it’s self-modified.
template <class NT>
BiPoly<NT> & BiPoly<NT>::differentiateX() {
if (ydeg >= 0)
for (int i=0; i<=ydeg; i++)
coeffX[i].differentiate();
return *this;
}//partial differentiation wrt X
In another member function called BiPoly::eval1(), I need to get the result of DifferentiateX() of the object who calls BiPoly<NT>::eval1(). Since DifferentiateX() is self-modified, I have to create a temp variable to get the result within eval1(). My question is: can I use "this" to create a temp object within member function? If so, how do I do that?
You can use copy constructor on
*this, yielding a copy of your polynomial object which you then differentiate and evaluate:Depending on how do you store the coefficients (e.g. in standard
vector), you might not even need to actually write the copy constructor.