Reputation: 492
I'm trying to understand this example we did in class but having some trouble...
For a class Time, an instance of this class is made up of hrs, mins secs
So
Time labStart(10,30,0);
Time labEnd (12,20,0);
(labEnd-labStart).printTime() //I'm not concerned with the printTime function
const Time Time::operator - (const Time& t2) const {
int borrow=0;
int s=secs-t2.secs;
if (s<0) {
s+=60;
borrow=1;
}
int m=mins-t2.mins2-borrow;
if (m<0) {
m+=60;
borrow=1;
}
else
borrow=0;
int h= hrs-t2.hrs-borrow;
if (h<0) {
h+=24;
Time tmp=Time(h,m,s);
return tmp;
}
So if we are passing both labEnd and labStart, and I was told (labEnd-labStart) ~ labEnd.operator-(labStart)
I dont understand how and where labEnd's variables are considered? In the function above only one parameter of Time is passed in, labStart, so the t2.mins t2.sec accounts for labStarts mins and secs,(30 mins and 0 sec respectively) however where is labEnd's variables (12,20,0)?? (instance variables hours, mins, secs)??
Upvotes: 1
Views: 53
Reputation: 780842
labEnd - labStart
is equivalent to:
labEnd.operator -(labStart)
So labEnd
is this
in the member function, and its member variables can be accessed as if they were ordinary variables.
Upvotes: 0
Reputation: 361585
In your function this
is a pointer to &labEnd
. The bare secs
, mins
, and hrs
mentions have an implicit this->
in front of them. If you write out the this->
's explicitly the three variable declarations become:
int s = this->secs - t2.secs;
int m = this->mins - t2.mins - borrow;
int h = this->hrs - t2.hrs - borrow;
Upvotes: 2