What is operator overhead?

In some programming languages like C++ it is possible to overload operators. What is it and what is it for?

Author: Avelino, 2015-05-02

3 answers

Operator Overloading is nothing more than changing the behavior of a language operator, such as +, -, etc.

Its function is more cosmetic, aimed at simplifying the reading and understanding of the code. But it allows to simplify the memory management in some cases (especially of immutable classes of arbitrary size, such as strings).

Internally, it is implemented as a traditional method, and therefore allows all optimizations and a method (in addition to being able to be simply replaced by one).

Applications

note: I will show as little code as possible, focusing on theory.

As said, the application of operator overload is basically cosmetic.

Example: imagine that you are implementing your class MyString and want to add concatenation functionality. You could then implement a MyString::concat method, and then use it:

s3 = s1.concat(s2);

Thanks to operator overload, you can implement a unique behavior for the Class MyString for the sum operator +, which performs concatenation. Thus, the above code could be rewritten as:

s3 = s1 + s2;

Another use, is the realization of the implicit type casting. This happens a lot in C++ where you can make the type std::string receive a char*:

std::string s = "array de char";

For this to work, the class std::string overloads the assignment operator = to receive a char* and return a std::string.

In this specific case, the overload is declared as:

string& operator= (const char* s);

Another interesting factor, is that you can have multiple behaviors for the operator, depending on The Associated operands. In the std::string class itself, the assignment operator has 3 overloads, each for a specific type of data:

string& operator= (const string& str); // [1]
string& operator= (const char* s);  // [2]
string& operator= (char c); // [3]

Thus, the following assignments are valid:

std::string s1 = "string"; // sobre-carga 2.
std::string s2 = 'a'; // sobre-carga 3.
std::string s3 = s1; // sobre-carga 1.

The implicit conversion of types is one of the biggest advantages of operator overload. Otherwise, methods like the following would have to be declared:

static string& fromStdString(const string& str); // ***
static string& fromCharArray(const char* s);
static string& fromChar(char c);

The first of the overloads is free! The compiler creates it automatically. But nothing prevents it from being written manually. This, in fact, is what occurs in std::string, as it is an immutable class and has reference counters to perform memory control.

Equivalently being used like:

std::string s1 = std::string::fromCharArray("string"); // sobre-carga 2.
std::string s2 = std::string::fromChar('a'); // sobre-carga 3.
std::string s3 = s1; // sobre-carga 1.

Conclusion

Over-load operators despite the complicated name is not extraterrestrial technology. It facilitates several things (especially memory management), but in none of the cases it is mandatory, although it can be facilitating.

 12
Author: Vinícius Gobbo A. de Oliveira, 2015-05-02 20:48:57

Normal operators are overloaded in C as well.

For example, the operator + serves to add integers (42 + 12), to add floating-point numbers (0.5 + 3.14159), to "advance" a pointer (string + 5), ...

But it is not possible to use + between, for example, a pointer and a double

string + 3.14169 // erro

Some programming languages allow you to define the operation to be performed when using an operator with operands that are not previously set.

So you can, for example, define that adding an integer to a list adds that integer to the end of the list (in C you would have to use a function for this).

// exemplo teorico: nao sei C++ nem outra linguagem com esta potencialidade
Lista seq = 42;
// print seq devolve 42
seq = seq + 3;
// print seq devolve 42, 3
 2
Author: pmg, 2015-05-02 18:17:24

Operator overload means to reset the operation that the symbol will perform as the Java operator + that for integers perform a sum and for Strings a concatenation the operator = in PHP that can have its function modified with magic methods.

 2
Author: Ricardo, 2015-05-02 18:43:56