【问题标题】:Why is returning an object reference using *this necessary in this case? [duplicate]为什么在这种情况下需要使用 *this 返回对象引用? [复制]
【发布时间】:2018-08-01 12:55:08
【问题描述】:

我正在学习的课程说返回此对象引用(使用 *this)主要用于链接等号(例如:b = d = c = a)但是,当我重载时,我无法理解这个返回是如何工作的'=' 运算符。 如果我不打算使用任何类型的链条,也有必要吗?这个返回语句是如何工作的?非常感谢。

class Rational {

    int _n = 0; // '_n' stands for numerator
    int _d = 1; // '_d' stands for denominator

public:

    Rational (int numerator = 0, int denominator = 1) : _n(numerator), _d(denominator) {};
    Rational (const Rational & rhs) : _n(rhs._n), _d(rhs._dd) {};

    ~Rational ();

    int numerator() const { retrun _n; };
    int denominator() const { return _d; };

    Rational & operator = (const Rational &);
    Rational operator + (const Rational &) const;
    Rational operator - (const Rational &) const;
    Rational operator * (const Rational &) const;
    Rational operator / (const Rational &) const;
};

Rational & Rational::operator = (const Rational & rhs) {
    if(this != &rhs){
        _n = rhs.numerator();
        _d = rhs.denominator();
    }
    return *this;
}

Rational Rational::operator + (const Rational & rhs) const {
    return Rational((_n * rhs._d) + (_d * rhs._n), (_d * rhs._d));
}

Rational Rational::operator - (const Rational & rhs) const {
    return Rational((_n * rhs._d) + (_d * rhs._n), (_d * rhs._d));
}

Rational Rational::operator * (const Rational & rhs) const {
    return Rational((_n * rhs._n), (_d * rhs._d));
}

Rational Rational::operator / (const Rational & rhs) const {
    return Rational((_n * rhs._d), (_d * rhs._n));
}

Rational::~Rational(){
    print("dtor: %d/%d\n", this->_n, this->_d);
    _n = 0; _d = 1;
}

std::ostream & operator << (std::ostream & o, const Rational & r){
    return o << r.numerator() << "/" << r.denominator();
}

int main(int argc, char** argv){

    Rational a = 7;                 // 7/1              
    cout << "a is: " << a << endl;
    Rational b(5, 3);               // 5/3
    cout << "b is: " << b << endl;
    Rational c = b;                 // Copy constructor
    cout << "c is: " << c << endl;
    Rational d;                     // Default constructor
    cout << "d is: " << d << endl;
    d = c;                          // Assignment constructor
    cout << "d is: " << d << endl;
    Rational & e = d;               // Reference
    d = e;                          // Assignment to self!
    cout << "e is: " << e << endl;

    cout << a << " + " << b << " = " << a + b << endl;
    cout << a << " - " << b << " = " << a - b << endl;
    cout << a << " * " << b << " = " << a * b << endl;
    cout << a << " / " << b << " = " << a / b << endl;

    return 0;
}

【问题讨论】:

  • 对`多次感到抱歉。我是 Stack Over Flow 的新手。
  • 没必要。这样做在语言中是惯用的,但不这样做(例如,将返回类型改为 void)会导致编译器在使用 a = b = c 时发出错误。

标签: c++ computer-science


【解决方案1】:

假设我们制作了两个Rational 来演示。

Rational r1(1,2), r2(2,3);

r1 = r2;

当我们这样做时,首先r1 将等于r2。然后操作将返回r1。就像任何函数调用一样,如果我们愿意,我们可以选择忽略返回值。

另一方面,我们也可以选择不忽略它。

Rational r1(1,2), r2(2,3), r3;

r3 = (r1 = r2);

括号强调第一个r1 = r2,它将返回r1。下一个r3 = r1

这和你使用函数的原理是一样的。

#include <iostream>

int print(int x) {
    std::cout << x << std::endl;
    return x;
}

void increase(int x) {
    std::cout << x + 5 << std::endl;
}

int main() {
    print(5); // We can ignore the return value, and just print the number

    int x = print(7); // We can print the number and store the value in a variable

    increase(print(3));  // We can print the value and pass it on to the increase function

    return 0;
}

【讨论】:

  • 我理解你的意思。那么,在这个函数中返回对象引用是不是基本没必要了?
  • @MauriceFigueiredo 仅当您想使用返回值时才需要。但是,对于operator=,大多数人都会期待它,因为它是在 c++ 中执行此操作的标准方式。
  • 太棒了!现在我完全明白了。
猜你喜欢
  • 1970-01-01
  • 2019-12-04
  • 2019-10-14
  • 1970-01-01
  • 2016-08-08
  • 2019-10-29
  • 2019-01-22
  • 2021-01-31
  • 1970-01-01
相关资源
最近更新 更多