[英]understanding temporary object in overloading the “/” operator
I try to understand how this line of code in blow c++ program, and my questions are 我试图了解打击C ++程序中的这一行代码,我的问题是
return Rational(_n * rhs._d, _d * rhs._n);
返回Rational(_n * rhs._d,_d * rhs._n);
C++ program: C ++程序:
#include <iostream>
class Rational {
int _n;
int _d;
public:
Rational ( int numerator = 0, int denominator = 1 ) : _n(numerator), _d(denominator) {};
Rational ( const Rational & rhs ) : _n(rhs._n), _d(rhs._d) {}; // copy constructor
inline int numerator() const { return _n; };
inline int denominator() const { return _d; };
Rational operator / ( const Rational & ) const;
};
Rational Rational::operator / ( const Rational & rhs ) const {
return Rational(_n * rhs._d, _d * rhs._n);
}
// useful for std::cout
std::ostream & operator << (std::ostream & o, const Rational & r) {
return o << r.numerator() << '/' << r.denominator();
}
int main( int argc, char ** argv ) {
using namespace std;
Rational a = 7; // 7/1
Rational b(5, 3); // 5/3
cout <<"a is : " <<a << endl;
cout << " b is : "<< b << endl;
cout << " a/b is: "<< a / b << endl;
return 0;
}
which has out put of 已经淘汰了
a is : 7/1
一个是:7/1
b is : 5/3b是:5/3
a/b is: 21/5a / b是:21/5
this program is simple version of this program in github 该程序是github中该程序的简单版本
To put it in a few words, the operator /
can be written like this: 简而言之,
operator /
可以这样写:
class Rational
{
//...rest of the stuff:
Rational Divide( const Rational & rhs) const
{
return Rational(_n * rhs._d, _d * rhs._n);
}
};
and instead of the operator /
for result = a/b
you can write result = a.Divide(b)
Basically operator /
behaves the same way as Divide
method: 而不是
operator /
对于result = a/b
您可以编写result = a.Divide(b)
基本上, operator /
行为与Divide
方法相同:
Now , let's analyze the divide method: 现在,让我们分析除法:
you have: 你有:
result = a.Divide(b); // which is the same as result = a/b in your case
and 和
Rational Divide( const Rational & rhs) const
{
return Rational(_n * rhs._d, _d * rhs._n);
}
rhs
is the argument passed to Divide
which is the variable b
. rhs
是传递给Divide
的参数,即变量b
。
_n
and _d
are the members of the variable a
. _n
和_d
是变量a
的成员。 You can also write them like this: this->_n
and this->_d
. 您也可以这样编写它们:
this->_n
和this->_d
。 Divide
is a member function so it can access _n
and _d
directly. Divide
是成员函数,因此它可以直接访问_n
和_d
。
Now to simplify this even further to understand the way it works, here is another way to write this: 现在,为了进一步简化它,以了解其工作方式,这是另一种编写方法:
class Radional {/*stuff*/};
Rational Divide( const Rational & a, const Rational & b)
{
return Rational(a._n * b._d, a._d * b._n);
}
For this example the result = a.Divide(b)
transforms into result = Divide(a,b)
Notice that now you have a._n
and a._d
which is the first argument of function Divide
对于此示例,
result = a.Divide(b)
转换为result = Divide(a,b)
注意,现在您有了a._n
和a._d
,这是Divide
函数的第一个参数
As a conclusion the expression a/b
is just a very nice way of writing Divide(a,b)
that c++ standard allows. 作为结论,表达式
a/b
只是编写c ++标准允许的Divide(a,b)
一种非常不错的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.