What is operator overload?

Asked

Viewed 5,893 times

11

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

3 answers

12


Over-loading operators is nothing more than altering the behavior of a language operator, as +, -, etc..

His role 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 possibilities of a method (besides being able to be simply replaced by a).

Applications

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

As stated, the application of over-load operators is basically cosmetic.

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

s3 = s1.concat(s2);

Thanks to the over-loading of operators, you can implement a unique behavior for the class MyString for the sum operator +, concatenation. In this way, the above code could be rewritten as:

s3 = s1 + s2;

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

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

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

In this particular case, the excess load is declared as:

string& operator= (const char* s);

Another interesting factor is that you can have various behaviors for the operator, depending on the associated operands. In the class itself std::string, the assignment operator has 3 over-loads, 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 allocations 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 over-loading operators. Otherwise, methods such as 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 over-loads is free! The compiler creates it automatically. But nothing prevents it from being written manually. This, in fact, is what happens in the std::string, because 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.

Completion

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

  • I found your answer very good, but I added one more question for those who start using this facility (like me), not falling into traps and/or making common mistakes.

  • 3

    For those who are learning C++ and have come this far, a very important reading is about Copy Builders.

  • Perfect. Thank you for the reply. :)

2

Normal operators are also overcharged in C.

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

But it’s not possible to use the + between, for example, a pointer and a double

string + 3.14169 // erro

Some programming languages allow defining 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 adds that integer at the end of the list (in C you 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

2

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.