What is operator overload?

10

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

    
asked by anonymous 02.05.2015 / 19:47

3 answers

11

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

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

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

Applications

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

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

Example: Imagine you are implementing your MyString class 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 overloading, you can implement a behavior unique to the MyString class for the + sum operator, which performs concatenation. Thus, the above code could be rewritten as:

s3 = s1 + s2;

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

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

For this to work, class std::string over-loads assignment operator = to receive char* and return std::string .

In this particular 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 class std::string 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]

So, 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.

Implicit type conversion is one of the major advantages of overloading operators. 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 automatically creates it. But nothing prevents it from being written manually. This, in fact, is what occurs in std::string , since it is an immutable class and has reference counters to perform memory control.

Equivalently being used as:

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

Overloading operators despite the complicated name is not extra-terrestrial technology. It facilitates various things (in particular memory management), but in neither case is it mandatory, although it can be a facilitator.

    
02.05.2015 / 20:20
2

Normal operators are overrun in C as well.

For example, the + operator is used to add integers ( 42 + 12 ), to add floating comma numbers ( 0.5 + 3.14159 ), to "forward" 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 defined.

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

// 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
    
02.05.2015 / 20:17
1

Operator overload means to redefine the operation that the symbol will perform as the + operator of Java that for integer numbers performs a sum and for strings a concatenation the = operator in PHP that can have its function modified with methods magicians.

    
02.05.2015 / 20:43