This can help if you think that statements are resolved one step at a time.
Take for example the following:
x = 1 + 2 * 3 - 4; x = 1 + 6 - 4; x = 7 - 4; x = 3;
C ++ does the same with function calls and everything else that you do inside the statement, solving each element inside in order of priority of the statement. Thus, you can think that your example is solved in the same way:
t.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); t.setMinute( 30 ).setSecond( 22 ); // hour is now set to 18 t.setSecond( 22 ); // minute is now set to 30 t; // seconds now set to 22
If you returned this instead of *this and thus indicating pointers instead of links, you will get the same effect, except that you replaced . to -> (as an example, you're doing it right using links). Similarly, if you returned a pointer or a link to another object, you can do the same with that. For example, let's say you have a function that returns a Time object.
class Time{ public: int getSeconds(){ return seconds; }; int seconds; }; Time getCurrentTime(){ Time time = doSomethingThatGetsTheTime(); return time; }; int seconds = getCurrentTime().getSeconds();
You get seconds without having to split the statement into two different statements or make a temporary variable to hold the returned time object.
This C ++ question : Using '.' the operator for expressions and function calls gets a little deeper if you want to read.
source share