-3

To preface this is homework and I'm not looking for answers, just a little direction because I can't make it to the lab tutor for help. My issue is that I've been given predefined class declarations that I can't change. I have 2 time objects that I need to add to equal a 3rd time object.

time3 = time2 + time1;

My overloaded function looks like this

Time Time :: operator + (const Time time2)
{
    Time::addSeconds(time1.second);
    Time::addMinutes(time1.minute);
    Time::addHours(time1.hour); //changes hrs and days

    return *this;
};

So I add in time1 and in my add functions I'm using the *this operator to assign which turns time2 into whatever time1 equals. So that doesn't work. I know how to do it if I could change my class definition, but I can't. I guess my intention was for *this to refer to time3, but it refers to the left hand side of the equation.

How can I make the result equal time3 in my add functions without changing the structure of my class?

I couldn't find any resources that show the proper way to handle this situation, but if you have any I'd be happy read through them.

Zach Morris
  • 367
  • 1
  • 4
  • 12
  • `*this` is definitely not an operator. And you should implement `operator+` by using `operator+=`. – chris Jun 11 '14 at 01:32
  • 1
    Where is time1 coming from? Also, is there no required text book for this class? – ChiefTwoPencils Jun 11 '14 at 01:32
  • Did you try searching before asking? Here's a pretty good write up on overloading operators right here on Stack OVerflow: http://stackoverflow.com/a/4421719/3651800. There's also quite a bit of info on the web – Matt Coubrough Jun 11 '14 at 01:35
  • 1. That operator should be `const`. 2. It should not operate on the object itself. -- it should construct a temporary value and return it. 3. There is no variable called `time1`. 4. Consider passing a const-reference instead of passing by value. – paddy Jun 11 '14 at 01:37
  • The linked question got everything you need. Especially the top answer under the section **Arithmetic Operators** subsection **Binary arithmetic operators** got everything you need. – AliciaBytes Jun 11 '14 at 01:37
  • I voted for reopening the question since the SO post marked as duplicate is too broad. It makes sense that the OP gets a concise answer to their particular, narrow, question. – R Sahu Jun 11 '14 at 02:59

1 Answers1

2

It looks like in this instance, this is a binary operator that treats both objects equally. This should be implemented as a free function. The structure would look like this:

Time operator + (const Time &time1, const Time &time2)
{
    Time t3 = time1; 
    t3.addSeconds(time2.second);
    t3.addMinutes(time2.minute);
    t3.addHours(time2.hour);
    //don't know how these functions work...

    return t3;
};
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • It's usually best not to answer duplicates, and as the original poster stated they're not looking for the code, just information. – Matt Coubrough Jun 11 '14 at 01:40
  • To be a free-standing / non-member function, you should remove the `Time::` prefix from `operator+`. It's not plausible for the `addSeconds` et al functions to work as you've postulated, because e.g. in `t3 = time2.addHours(time1.hour);` there's no reference way the right-hand-side expression can access the existing value ot `t3` to factor in earlier adds. I'd hazard that it needs to be `operator+(Time time1, const Time& time2) { time1.addSeconds(time2.second); time1.addMinutes(time2.minute); time1.addHours(time2.hour); return time1; }` - the by-value parameter deliberately makes a copy. – Tony Delroy Jun 11 '14 at 01:54