How does this cascading work

0 votes

The following class interface I have is:

  class Time
  {
  public:
     Time( int = 0, int = 0, int = 0 ); 
     Time &setHour( int );                 
     Time &setMinute( int );               
     Time &setSecond( int ); 

  private:
     int hour; 
     int minute; 
     int second; 
  }; 

The implementation is here:

  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; 
  } 


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

In my main .cpp file, I have this code:

int main()
{
    Time t;     
    t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
    return 0;
}

How are these function calls connected in a chain? 

I'm not sure why this works.

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
618 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

This functions as intended since when you call

t.setHour( 18 )

A Time&, a reference to a Time object, is the return value. 

What's more, it's described as

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

This is a pointer to the object on which the call was made within a member function, and *this is a reference to the object on which the call was made (the receiver object). 

To put it another way, when you call setHour, the function adds the hour to the time and then returns a reference to the Time object on which you called. 

As a result, when you use t.setHour(18), you'll also set the hour and get a reference to the receiver object. 

You could then write.

t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );

because it's interpreted as

((t.setHour( 18 )).setMinute( 30 )).setSecond( 22 );

and in each case the function returns a reference to t.

More broadly, every action you perform on the function's return value is identical to operations you would conduct on the object itself whenever a function returns a reference and that reference is *this.

Hope this is useful!

answered Aug 19, 2022 by Damon
• 4,960 points

edited 6 days ago

Related Questions In C++

0 votes
1 answer

How does #include <bits/stdc++.h> work in C++? [duplicate]

#include <bits/stdc++.h> is a precompiled header implementation ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
8,836 views
0 votes
0 answers

How does c++ std::vector work?

How do the addition and removal of ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
685 views
0 votes
1 answer

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

You desire: (Achievable with virtual inheritance) ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,230 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
781 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
630 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
971 views
0 votes
1 answer

C++ `this` pointer

Pointer variables are used to store the ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
677 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
551 views
0 votes
1 answer

*this vs this in C++

This is a pointer, and *this is a pointer that has been dereferenced. If you had a function that returned this, it would be a pointer to the current object, but a function that returned *this would be a "clone" of the current object, created on the stack unless you defined the method's return type to be a reference. A small application that demonstrates the difference between working with copies and working with references: #include <iostream> class Foo { public: ...READ MORE

answered Jun 28, 2022 in C++ by Damon
• 4,960 points
865 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
781 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP