简体   繁体   English

了解重载“ /”运算符中的临时对象

[英]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);

  1. how the _n refer to the first object data member? _n如何引用第一个对象数据成员?
  2. how the temporary object refer get the a._n? 临时对象如何引用得到a._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/3 b是:5/3
a/b is: 21/5 a / 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->_nthis->_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._na._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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM