Polynomial operator + (const Polynomial& p);
//Adds existing Polynomial to p, returning the result
Run Code Online (Sandbox Code Playgroud)
因为我们无法改变const的值,有没有办法制作这个p的副本以允许我访问p的函数?
对不起工作的人,并且完全显示整个程序,但在私有字段的Poly.H中,它显示为:private:List <Term> poly
所以在PolyTest.cpp中
如果我需要两个多项式p_add = poly1 + poly2
我需要在operator +中编写函数来添加类似的术语.希望这是有帮助的.
如果您需要制作副本,则只需将其复制到该功能中即可
Polynomial operator + (const Polynomial& p);
{
Polynomial copy = p;
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
现在,只是因为pIS const并不意味着你不能使用它的成员/功能.典型的operator+看起来像
Something operator + (const Something& s);
{
return Something(this->some_memeber + s.some_memeber,
this->another_memeber + s.another_memeber);
}
Run Code Online (Sandbox Code Playgroud)