-1

I have a problem. I know how to overload += for two classes of the same type but I have no idea how to do it for int's. What I mean is: lets say I have a class Person made of char *name and an int age.

Let a = Person("Random name","12")

If I do a += 4, I want it to do the age = age + that number (12 + 4 = 16).

The problem is I don't know how to write the code inside the += function.

The code is a bit too long to write it here but I think I explained well enough my problem.

Vzlom -
  • 15
  • 5
  • 4
    You can simply define the corresponding version of `Person::operator+=` as `Person& operator+=(int a) { age += a; return *this; }`. – Daniel Langr May 18 '22 at 10:45
  • 3
    The operator overloads are just normal functions with peculiar names. If the function were called "add_age" rather than "operator +=", would you know what to write? (That is, you have `Person& Person::add_age(int years) { ... something ... }`.) – molbdnilo May 18 '22 at 11:04

0 Answers0