What is operator overloading in C++?

Hey guys,

I am trying to understand this topic. I watched dozens of videos, read articles about it but still can’t understand.

Can you explain it to me like if I were a child please?

Best Regards

1 Like

It means that you can change the meaning of operators such as “+” or “-”.

C++ has a lot of pre-defined behaviours for operators, for example if you write int a = 2+2 the compiler will understand that + means addition and that a = 4.

Sometimes you might have a more complicated situation.

Imagine you have a Person class that represents a person.
You might have two objects of that class, for example Person p1, p2.
You can custom script the plus-sign (+) to have custom behaviour when you add two objects of class Person together.

For example when you write p1+p2 maybe you want the result to be a new project of type Couple.

When you do that you overload the + operator because you change the default behaviour.

1 Like

Thank you very much Ivan!

Do you use operator overloading frequently in your projects as an experienced developer?