重载运算符返回错误的值 [英] overloaded operators returning wrong values
问题描述
我已经覆盖了我的所有算术运算符但是所有的都是
作为乘法运算。下面是添加的示例
运算符:
Rational运算符+(const Rational& r1,const Rational& r2){
//后置条件:返回r1和r2之和
int numerator;
int分母;
denominator =(r1.getDen()* r2.getDen());
numerator =(r1.getNum()* r2.getDen()+ r2.getNum()*
r1.getDen());
理算和(分子,分母);
返还金额;
}
i have overlaoded all of my arithmetic operators but all are
functioning as multiplication. below is a sample of the addition
operator:
Rational operator + (const Rational& r1, const Rational& r2){
//Postcondition: sum of r1 and r2 are returned
int numerator;
int denominator;
denominator = (r1.getDen() * r2.getDen());
numerator = (r1.getNum() * r2.getDen() + r2.getNum() *
r1.getDen());
Rational sum (numerator, denominator);
return sum;
}
推荐答案
andrew browning写道:
andrew browning wrote :
我已经覆盖了所有的算术运算符,但都是
作为乘法运行。下面是添加运算符的示例:
Rational运算符+(const Rational& r1,const Rational& r2){
//后置条件:r1和r2的和是返回
int numerator;
int denominator;
denominator =(r1.getDen()* r2.getDen());
numerator =( r1.getNum()* r2.getDen()+ r2.getNum()*
r1.getDen());
有理和(分子,分母);
返回总和;
}
i have overlaoded all of my arithmetic operators but all are
functioning as multiplication. below is a sample of the addition
operator:
Rational operator + (const Rational& r1, const Rational& r2){
//Postcondition: sum of r1 and r2 are returned
int numerator;
int denominator;
denominator = (r1.getDen() * r2.getDen());
numerator = (r1.getNum() * r2.getDen() + r2.getNum() *
r1.getDen());
Rational sum (numerator, denominator);
return sum;
}
您的问题是什么?
这是对我的正确补充。
And your problem is ?
This is correct addition of rationals to me.
以下是重载算术和
赋值运算符的推荐方法:
T& T :: operator + =(const T&){
// .... impl
return * this
}
T操作符+(const T& lhs,const T& rhs){
T temp(lhs);
return temp + = rhs;
}
----------------------------- -------------------------------------------------- ---------
David Maisonave
http:/ /axter.com
C ++专家交流的十大成员:
http://www.experts-exchange.com/Cplusplus
------------- -------------------------------------------------- -------------------------
The following is the recommended approach to overloading arithmetic and
assignment operators:
T& T::operator+=(const T&){
//.... impl
return *this
}
T operator+(const T& lhs, const T& rhs){
T temp(lhs);
return temp += rhs;
}
----------------------------------------------------------------------------------------
David Maisonave
http://axter.com
Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------
Axter写道:
Axter wrote:
以下是重载算术和
赋值运算符的推荐方法:
T& T :: operator + =(const T&){
// .... impl
返回*这个
}
T操作符+(const T& lhs, const T& rhs){
T temp(lhs);
return temp + = rhs;
}
The following is the recommended approach to overloading arithmetic and
assignment operators:
T& T::operator+=(const T&){
//.... impl
return *this
}
T operator+(const T& lhs, const T& rhs){
T temp(lhs);
return temp += rhs;
}
推荐给谁原因?
我希望看到推荐的为矩阵进行的方法
乘法或任意精确整数乘法。在
案例中,就地实施远非明显(如果可能);对于矩阵或BigInt来说* *
实现*可能会产生内部临界数量比实现* =更多的临时数据*。
假设有一个有效的交换方法(比如std :: vector),
错误是什么:
T operator +( T const& a,T const& b){
// impl ...
返回(结果);
}
T& operator + =(T const& t){
T temp = * this + t;
swap(* this,temp);
返回(* this);
}
另请注意,如果分析显示需要使用表达式模板,
an实现* =就*来说更自然,另一种方式
左右 - 实际上,我看不出我怎么写一个表达模板
for *就* =而言;但这可能是我缺乏想象力。
最好
Kai-Uwe Bux
Recommended for which reason?
I would like to see the "recommended" approach carried out for matrix
multiplication or arbitrary precission integer multiplication. In both
cases, an in-place implementation is far from obvious (if possible); and
implementing * in terms of *= for matrices or BigInt is likely to create
more temporaries internally than implementing *= in terms of *.
Assuming there is an efficient swap method (like for std::vector), what is
wrong with:
T operator+ ( T const & a, T const & b ) {
// impl ...
return ( result );
}
T & operator+= ( T const & t ) {
T temp = *this + t;
swap( *this, temp );
return ( *this );
}
Also note that, if profiling shows the need for using expression templates,
an implementation of *= in terms of * is more natural that the other way
around -- in fact, I do not see how I would write an expression template
for * in terms of *=; but that could be a lack of imagination on my part.
Best
Kai-Uwe Bux
这篇关于重载运算符返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!