简体   繁体   English

返回* this,尝试级联,不返回引用

[英]Returning *this, attempting cascading, and not returning a reference

I have a question that is similar to the question that was asked here: How does "this" cascading work? 我有一个与此处提出的问题类似的问题: “此”级联如何工作?

Suppose I have the following code: 假设我有以下代码:

#include <iostream>
using namespace std;

class Time
  {
  public:
     Time( int = 0, int = 0, int = 0 );
     Time setHour( int );
     Time setMinute( int );
     void print( void );

  private:
     int hour;
     int minute;
  };

  Time::Time(int hr, int mn, int sc)
  {
     hour = hr;
     minute = mn;
  }

  void Time::print( void )
  {
     cout << "hour = " << hour << endl;
     cout << "minute = " << minute << endl;
  }

  Time Time::setHour( int h )
  {
     hour = ( h >= 0 && h < 24 ) ? h : 0;
     return *this;
  }


  Time Time::setMinute( int m )
  {
     minute = ( m >= 0 && m < 60 ) ? m : 0;
     return *this;
  }

int main()
{
   cout << "Hello, world!" << endl;
   Time t;
   t.setHour( 10 ).setMinute( 25 );
   t.print();
}

Then, it is clear that the function setMinute( 25 ) is not running on the Time object t. 然后,很明显,函数setMinute(25)不在时间对象t上运行。 Note that the functions setHour and setMinute do not return references to Time objects. 请注意,函数setHour和setMinute不会返回对Time对象的引用。

What is happening after t.setHour( 10 ) executes? t.setHour(10)执行后会发生什么? Does the function setHour somehow return a "copy" of the object t, and setMinute( 25 ) is running on the copy? 函数setHour是否以某种方式返回对象t的“副本”,并且setMinute(25)正在副本上运行? I have compiled the program with -Wall and no errors or warnings are returned. 我已经用-Wall编译了程序,没有错误或警告返回。

Thanks for your assistance. 谢谢你的协助。

Your analysis seems correct. 您的分析似乎是正确的。 This expression 这个表达

t.setHour( 10 )

returns a temporary Time object. 返回一个临时的Time对象。 You then call setMinute(25) on that temporary. 然后,在该临时目录上调用setMinute(25) This in turn returns another temporary Time object, which is not assigned to anything. 依次返回另一个临时的Time对象,该对象未分配给任何对象。 So setHour() acts on the t instance, but setMinute() acts on a temporary, which disappears at the end of this line 因此setHour()作用于t实例,而setMinute()作用于临时对象,该临时对象在此行的末尾消失

t.setHour( 10 ).setMinute( 25 );

Each of t's methods return a reference to t. t的每个方法都返回对t的引用。 A reference is an alias. 引用是别名。 So if you did 所以如果你做了

Time t;
 Time& tAgain = t;
 tAgain.setMinute( 25); 
tAgain.setMinute also alters t's time.

Now extrapolate that simple example to cascading. 现在将这个简单的例子推算为级联。 Each method of t returns a reference to itself t的每种方法都返回对自身的引用

Time &Time::setSecond( int s ) 
  {
      second = ( s >= 0 && s < 60 ) ? s : 0; 
      return *this; 
  }

So in the expression: 所以在表达式中:

   t.setHour( 10 ).setMinute( 25 )

t.setHour( 10 ) calls setHour on t, then returns a reference to t. t.setHour( 10 )在t上调用setHour,然后返回对t的引用。 In this case the reference is temporary. 在这种情况下,参考是临时的。 So you can think of it as if the above line changed to the following on evaluating setHour: 因此,您可以将其视为在评估setHour时,上述行变为以下内容:

tAgain.setMinute(25);

t.setHour returned a reference -- similar to our tAgain above. t.setHour返回了一个参考-与上面的tAgain类似。 Just an alias for t itself. 只是t本身的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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