I'm working on a project where I need to add two objects together by overloading the addition operator, 99% of it makes sense to me, but I can't figure out how to do the actual addition process.
My code is currently
Time operator+(const Time& t1)
{
int num = this.milliseconds + t1.milliseconds;
Time t(num);
return t;
}
Then I call it like so
t4 = t1 + t2;
I thought using this.milliseconds would allow me to access t1's int variable but it won't allow me.
Basically my question is how do I access the time variable on the left of the + operator since I only pass the operator+ function one object of Time? (t2)
thisis a pointerthis., if it is part of theTimeclass. You should make that member functionconst, or better, make it a free function of two arguments using the class'operator+=.Timeinstance directly in thereturnstatement, e.g.return {num};.numaconstvariable.