Beginner to Intermediate: Operator Overloading (I)
Introduction
In my previous article, "From Basic to Intermediate: Queues, Lists, and Trees (VIII)", I presented my version of one of the many tree-balancing algorithms that can be implemented. As explained there, the goal of every balancing algorithm is to make the tree more efficient for searching. Some of them allow the tree to be better adapted to certain types of searches, while others make it more suitable for other kinds of searches.
For now, we will not go into detail about searching in trees. Before I demonstrate this type of implementation, I want to introduce and explain one more topic. This is a rather interesting topic, although it often causes confusion for many beginner programmers, especially when they are learning other languages that do not offer this capability.
The feature we are about to discuss is intended to simplify further code development in various ways. As code becomes more complex, it often loses readability, which makes it difficult to understand what the intended goal is.
However, contrary to what many people think, what we will see here will make programming much more exciting and enjoyable. Nevertheless, certain precautions must be taken when implementing it. I will try to keep things as simple as possible whenever I can. Most of what I will be explaining is based on my experience programming in C/C++. Nevertheless, I will try to explain everything as simply as possible.
Now let's move on to the main topic of this article.
Operator Overloading (I)
We often end up writing code that does not seem to make much sense at first. However, as we develop the code and create various functions and procedures, we may encounter situations that, at first glance, seem quite difficult to resolve. The real challenge lies not in the complexity of writing the code itself, but in making it as clear as possible — not only for the person who writes it, but also for those who might want to study it in order to improve it or, perhaps, reuse part of the code that has already been developed or part of code written previously.
The main problem with most of the implementations we have to work on is the large number of functions and procedures involved. And the real problem isn't so much that we use functions or procedures, but rather that in many cases they can be replaced by a different way of writing code or a different code structure. However, without the proper knowledge, it is very difficult to understand how to perform such a replacement correctly. As a result, we find ourselves forced to use an implementation that, over time, turns what initially seemed simple into an extremely convoluted and disjointed structure — or into an extremely convoluted and fragmented set of solutions — making it much harder to understand the code correctly.
If you have been following my articles, you have probably noticed that I try to avoid using certain tools so I do not have to constantly explain how they work. However, since this series of articles is intended to convey a different kind of knowledge, I see no reason to limit myself to using simple tools. We can delve a little deeper into some of the language's more advanced features. This way, in the future, I will be able to write code that, in my opinion, makes it easier to analyze what's being implemented, without having to explain every detail, since we will be covering those details in these articles.
All right, although I know that many of you may be interested only in creating some kind of indicator or Expert Advisor and think that there is not much point in learning what I am going to talk about here, I also know that others will be able to put what we are about to learn to very good use. In fact, it seems to me that a good programmer always has tools in their arsenal that they almost never use. Nevertheless, the knowledge gained from trying to understand why something is done one way rather than another can play a decisive role in more complex situations. Therefore, a proper understanding of how operator overloading works can help you make the code you are writing much easier to understand. And the easier the code is to understand, the faster it can be fixed, and the easier and more effective it will be to make improvements to the system you are developing.
To get off to the right start, you need to understand the following: operator overloading is often built right into the programming language itself. And you use it often without even realizing it. Isn't that right? All right, let's look at a very simple example that uses operator overloading. The following code implements this example.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. void OnStart(void) 05. { 06. string sz1; 07. 08. sz1 = "Hello world."; 09. sz1 = sz1 + "How has your day been ?"; 10. 11. Print(sz1); 12. } 13. //+------------------------------------------------------------------+
Code 01
By now, I think everyone already knows what will be displayed in the MetaTrader 5 terminal when Code 01 is executed. Therefore, there is no need to show the result. However, this does not change the fact that Code 01 uses operator overloading, even though you may not notice it. This happens on the ninth line, when we apply the addition operator to two string operands. But wait a minute. This is not operator overloading, because I am telling the compiler to combine both string values into a single, longer string. That is why I do not consider this to be operator overloading. Although, in fact, I still do not know exactly what it is. I am relying solely on what was explained when I studied the article on overloading functions and procedures.
Well, my dear reader, as strange as it may seem, in MQL5 — just like in other languages — some operators are overloaded by the language itself. All of this is designed so that you can easily perform certain operations, making the code more readable and understandable. Because this mechanism is built into the language, the expression on the ninth line of Code 01 can replace an explicit call to a function or procedure that concatenates string values. However, strictly speaking, the addition operator is not intended for string concatenation. Its purpose is to add two values together and obtain a third. However, thanks to operator overloading implemented at the language level, the compiler knows how to use the addition operator to concatenate string values.
The following code is equivalent to Code 01 but does not use operator overloading.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. void OnStart(void) 05. { 06. string sz1; 07. 08. sz1 = "Hello world."; 09. StringConcatenate(sz1, sz1, "How has your day been ?"); 10. Print(sz1); 11. } 12. //+------------------------------------------------------------------+
Code 02
Please note that Code 02 produces the same result as Code 01. The only difference between them is in the ninth line. Now take a look at both options and answer completely honestly: which one makes it easier to understand exactly what is going on? I think most people would say it is Code 01. This is precisely what operator overloading allows us to do: use an operator to express an operation that would otherwise require an explicit call to a function or procedure, making the code more readable.
Now, please take a look at the following. In principle, there is no strict rule for implementing operator overloading. There is only what might be called a "gentlemen's agreement" to avoid absurd situations. For example, when you see an addition operator, you do not expect division or subtraction to be performed instead. Nevertheless, there is nothing stopping you from implementing something like this. The problem is that the code will become extremely difficult to understand — even for you, my dear reader, as its author. In the end, something that could have been interesting would be completely forgotten. Therefore, when it comes to operator overloading, it is always best to exercise caution.
So, if the language allows it, we can implement operator overloading. The number of operators that can be overloaded varies from language to language. In the case of MQL5, the operators that can be overloaded are shown in the following figure.

Figure 01
It is very important to know which operators the language allows you to overload. The thing is, depending on how the code is implemented, you may have to look through it to determine what operation a particular operator actually performs on the data.
All right, as you can see, the operators are divided into small groups. This greatly simplifies the implementation of operator overloading, since the overloads within each group of operators have very similar structures. Essentially, overloading is declared using the simple syntax shown below.
return_type operator overloaded_operator (parameter list)
Please take note of the information above, dear reader, because this is the syntax we will ALWAYS have to use whenever we need or want to overload an operator. Now let's break down what each element means. RETURN_TYPE specifies the type of value that the operator function will return. If a function does not return a value, we will use the void type, though we will examine this in practice a little later. OPERATOR is a reserved word in the language that tells the compiler that an operator function is being declared. OPERATOR refers to the operator that will be overloaded.
To find out which operators can be overloaded, see Figure 01. The list of parameters works the same way as in the cases discussed in the first articles. The only difference is that, depending on the operator, there may be one or more parameters here. At this stage, understanding how operators are grouped helps us interpret the parameter list itself. Operators in the binary group require only one parameter, whereas operators in the unary group require none. However, other operators may use a different number of parameters. But we will come back to that later. There is a time for everything.
Great, but how do you put that into practice? Well then, my dear reader, here comes the most interesting part. In general, the operator overloads we define will primarily be used with user-defined types. Therefore, at this stage, it is essential that you have a solid understanding of the concept of structures.
If you have just joined us, we have already talked about structures. You can start with the article "From Basic to Intermediate: Struct (I)". There, we began to explain what a structure is and how it can be used. However, I will assume that you already know what a data structure is. This allows us to focus on operator overloading.
A good way to start understanding operator overloading is to first understand the concept of overloading itself. This concept was introduced in the article "From Basic to Intermediate: Overloading". However, in that discussion, we only talked about function and procedure overloading. Understanding this will help you take the next step. So, let's start by looking at what can be considered the classic model for representing operator overloading. The following code implements this model.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. double m_r, m_i; 07. //+----------------+ 08. stComplex(): m_r(0), m_i(0) {} 09. //+----------------+ 10. stComplex(double r, double i): m_r(r), m_i(i) {} 11. //+----------------+ 12. stComplex add(const stComplex &arg1, const stComplex &arg2) 13. { 14. return stComplex(arg1.m_r + arg2.m_r, arg1.m_i + arg2.m_i); 15. } 16. //+----------------+ 17. }; 18. //+------------------------------------------------------------------+ 19. void OnStart(void) 20. { 21. stComplex a(2, 5), 22. b(8, -3), 23. c; 24. 25. c = c.add(a, b); 26. 27. PrintFormat("C = %.02f %c %.02fi", c.m_r, (c.m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 28. } 29. //+------------------------------------------------------------------+
Code 03
"Hmm, this code seems really confusing to me. Are you sure it is as simple as you said?" Of course, this code is simple, my dear reader. If this seems unclear to you, it means you have not studied or read the previous articles. In that case, I recommend that you go back and study them before trying to understand what we are going to do here. Without an understanding of the basics, none of this will make any sense at all.
So, when we run Code 03, we will get the result shown in the following figure.

Figure 02
As you can see, it is not hard at all to understand what is going on here. However, we are not yet using operator overloading. I just want to show, step by step, how it all works. And now, my dear reader, please take note of the following. On line 25, we specify that we need to add two values together and store the result in another variable. As it is currently implemented, it is clear that adding a third value to the calculation would be quite cumbersome. To do that, we would have to add a few more calls to the add function defined on line 12. Ultimately, the code would become quite confusing due to the growing number of calls. However, if we make a small change, the situation will be completely different, since we will be able to use the addition operator to add two complex numbers, just as the add function defined on line 12 does.
"But wait. Are you saying that the addition operator can be used in Code 03?" Yes, dear reader, that is exactly what I want to say. However, I want you to understand exactly what I am saying. Therefore, we will modify the code by replacing the call to the add function on line 25 with the addition operator. The following code implements this change.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. double m_r, m_i; 07. //+----------------+ 08. stComplex(): m_r(0), m_i(0) {} 09. //+----------------+ 10. stComplex(double r, double i): m_r(r), m_i(i) {} 11. //+----------------+ 12. stComplex add(const stComplex &arg1, const stComplex &arg2) 13. { 14. return stComplex(arg1.m_r + arg2.m_r, arg1.m_i + arg2.m_i); 15. } 16. //+----------------+ 17. }; 18. //+------------------------------------------------------------------+ 19. void OnStart(void) 20. { 21. stComplex a(2, 5), 22. b(8, -3), 23. c; 24. 25. c = a + b; 26. 27. PrintFormat("C = %.02f %c %.02fi", c.m_r, (c.m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 28. } 29. //+------------------------------------------------------------------+
Code 04
Wait a minute. Does Code 04 work? No, my dear reader. If you try to compile it, you'll get the following compiler errors.

Figure 03
This is because the compiler DOES NOT KNOW how to add two complex numbers. Or, more precisely, it does not know how to handle a data type created by a user or a programmer. Nevertheless, even in this case, we can take a different approach, keeping in mind that each situation is unique. In that case, we could use the following code.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. double m_r, m_i; 07. //+----------------+ 08. stComplex(): m_r(0), m_i(0) {} 09. //+----------------+ 10. stComplex(double r, double i): m_r(r), m_i(i) {} 11. //+----------------+ 12. stComplex add(const stComplex &arg1, const stComplex &arg2) 13. { 14. return stComplex(arg1.m_r + arg2.m_r, arg1.m_i + arg2.m_i); 15. } 16. //+----------------+ 17. }; 18. //+------------------------------------------------------------------+ 19. void OnStart(void) 20. { 21. stComplex a(2, 5), 22. b(8, -3), 23. c; 24. 25. c = stComplex(a.m_r + b.m_r, a.m_i + b.m_i); 26. 27. PrintFormat("C = %.02f %c %.02fi", c.m_r, (c.m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 28. } 29. //+------------------------------------------------------------------+
Code 05
I know that line 25 in Code 05 might seem pretty strange to you, but it really does work. This is because we use the constructor from line 10 to create a value of the complex type. This allows the compiler to work with a data type it originally could not handle. This example allows us to examine another operation that many people consider relatively simple, but which, depending on the situation, can turn out to be quite complex: assigning a value to a variable using the assignment operator. As a general rule, a variable can only be assigned values of the same type. However, there are situations when we need to assign a value of one type to a variable of a completely different type. If necessary, we will have to overload the assignment operator; otherwise, the code will not work.
But let's get back to our original question: our first implementation of operator overloading. As we have already seen, there are various ways to add two complex numbers. However, Code 04 does not currently allow this operation to be performed. Now I would like to ask you: how could we perform this addition? Well, the answer is at the beginning of the article. "But how can that be? I do not understand that." Okay, remember when I mentioned that there is a special syntax for operator overloading?
So, if we already have a function or procedure that performs the operation we need, we can reuse its implementation to overload the operator. It is not enough to simply change the name: you need to modify its declaration to match the syntax return_type operator overloaded_operator (parameter list). This is exactly what happens with the function defined on line 12 of Code 03, which adds two complex numbers and returns the result. If you replace its declaration with one using operator +, the compiler will recognize it as operator overloading for addition, and Code 04 will work. You might be thinking, "But that sounds pretty complicated. How do I tell the compiler to do that?" Well, that is actually the easiest part. Compare the implementation of Code 04 with the implementation of the following code.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. double m_r, m_i; 07. //+----------------+ 08. stComplex(): m_r(0), m_i(0) {} 09. //+----------------+ 10. stComplex(double r, double i): m_r(r), m_i(i) {} 11. //+----------------+ 12. stComplex operator+(const stComplex &arg1) 13. { 14. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 15. } 16. //+----------------+ 17. }; 18. //+------------------------------------------------------------------+ 19. void OnStart(void) 20. { 21. stComplex a(2, 5), 22. b(8, -3), 23. c; 24. 25. c = a + b; 26. 27. PrintFormat("C = %.02f %c %.02fi", c.m_r, (c.m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 28. } 29. //+------------------------------------------------------------------+
Code 06
"I do not understand. Code 06 really is very similar to Code 04. However, line 12 uses a different number of arguments. Why?" The thing is, line 25 uses the addition operator. However, when using operator overloading, line 25 should be interpreted differently. And this is exactly where many beginners feel completely lost when they come across code that uses operator overloading. To a beginner, an expression with the addition operator looks like ordinary addition, not a call to an overloaded function. However — and this is something you need to understand, my dear reader — the compiler will not interpret line 25 the way it is written in Code 06. In fact, the compiler interprets it as the equivalent of the following code.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. double m_r, m_i; 07. //+----------------+ 08. stComplex(): m_r(0), m_i(0) {} 09. //+----------------+ 10. stComplex(double r, double i): m_r(r), m_i(i) {} 11. //+----------------+ 12. stComplex operator+(const stComplex &arg1) 13. { 14. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 15. } 16. //+----------------+ 17. }; 18. //+------------------------------------------------------------------+ 19. void OnStart(void) 20. { 21. stComplex a(2, 5), 22. b(8, -3), 23. c; 24. 25. c = a.operator+(b); 26. 27. PrintFormat("C = %.02f %c %.02fi", c.m_r, (c.m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 28. } 29. //+------------------------------------------------------------------+
Code 07
Code snippets 06 and 07 are equivalent. However, it is much more common to write it as shown in Code 06. Nevertheless, as a programmer, you must interpret and understand the code in the same way that a compiler would. To do this, we need to examine the equivalent form of Code 07, where line 25 calls the function on line 12. I know that at first it seems much more complicated than it actually is. Nevertheless, this correspondence helps us understand why we need to pass only one argument to the function in line 12. Otherwise, it would be very difficult to explain it any other way.
"Hmm, great. I really enjoyed learning something new just now. This operator overloading really does seem very interesting. But tell me, is this the only way to add two complex numbers? Isn't it possible, for example, to add a complex number and an integer?"
Okay, this is a little harder to understand. However, the answer is YES. We can add a value of type stComplex and an integer. However, it is important to distinguish between two different expressions: stComplex + an integer, where the stComplex object is the left operand, and an integer + stComplex, where an integer is in that position. However, to make the explanation easier — since this point may not be immediately clear — we will change one aspect of the stComplex structure. To begin with, it is worth noting that we are allowing access to internal variables from outside the structure, which, generally speaking, is not the best idea. Therefore, we will modify the code to hide these variables inside the structure. After that, we will be able to implement the stComplex + integer operation first.
The first part of the changes is shown below.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. //+----------------+ 07. private : 08. double m_r, m_i; 09. //+----------------+ 10. public : 11. //+----------------+ 12. stComplex(): m_r(0), m_i(0) {} 13. //+----------------+ 14. stComplex(double r, double i): m_r(r), m_i(i) {} 15. //+----------------+ 16. stComplex operator+(const stComplex &arg1) 17. { 18. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 19. } 20. //+----------------+ 21. void Debug(void) 22. { 23. PrintFormat("Internal Value = %.02f %c %.02fi", m_r, (m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 24. } 25. //+----------------+ 26. }; 27. //+------------------------------------------------------------------+ 28. void OnStart(void) 29. { 30. stComplex a(2, 5), 31. b(8, -3), 32. c; 33. 34. c = a + b; 35. c.Debug(); 36. } 37. //+------------------------------------------------------------------+
Code 08
So, let's now check whether it is possible to evaluate the expression stComplex + an integer — that is, a sum where the left operand is of type stComplex and the right operand is an integer. To do this, you first need to understand how to implement function and procedure overloading. At the beginning of the article, I mentioned that this knowledge would be useful, and I indicated which article contains this information. Once you learn how to implement this kind of overloading, we will be able to define the operation for these types and this operand order. To do this, we simply need to define the following procedure.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. //+----------------+ 07. private : 08. double m_r, m_i; 09. //+----------------+ 10. public : 11. //+----------------+ 12. stComplex(): m_r(0), m_i(0) {} 13. //+----------------+ 14. stComplex(double r, double i): m_r(r), m_i(i) {} 15. //+----------------+ 16. stComplex operator+(const stComplex &arg1) 17. { 18. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 19. } 20. //+----------------+ 21. stComplex add(const double arg2) 22. { 23. return stComplex(m_r + arg2, m_i); 24. } 25. //+----------------+ 26. void Debug(void) 27. { 28. PrintFormat("Internal Value = %.02f %c %.02fi", m_r, (m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 29. } 30. //+----------------+ 31. }; 32. //+------------------------------------------------------------------+ 33. void OnStart(void) 34. { 35. stComplex a(2, 5), 36. b(8, -3), 37. c; 38. 39. c = a + b; 40. c.Debug(); 41. 42. c = c.add(4); 43. c.Debug(); 44. } 45. //+------------------------------------------------------------------+
Code 09
When you run Code 09, you will get the result shown in the following figure.

Figure 04
This clearly shows that Code 09 allows the expression stComplex + an integer: the left operand is of type stComplex, and the right operand is an integer value. However, this raises a question, my dear reader, and that is precisely where the difficulty lies. Therefore, in Code 09, we have not yet implemented operator overloading directly. First of all, I want you to understand one thing: rearranging the operands results in a different expression. An implementation that works for stComplex + an integer does not necessarily work for an integer + stComplex, and it is precisely this difference that determines whether the code will compile.
Please note the following: when the compiler analyzes the code to generate an executable file, it will need to perform overload resolution based on the type and position of each operand. Therefore, stComplex + an integer and an integer + stComplex are not interchangeable: in the first expression, stComplex is the left operand, while in the second, the integer is the left operand. If overloading is not defined for the operand order used, the compiler WILL NOT generate an executable file, since it will be unable to resolve this operation. Therefore, understanding function and procedure overloading is important for making broader use of operator overloading. You might think that one has nothing to do with the other, but everything is interconnected. Therefore, the concepts that initially seemed to make little sense now need to be properly understood, because they are beginning to come together to form a complete picture.
Now, take note: when transitioning from Code 03 to Code 06, we remove one argument from the call. To support the reverse order as well, we need to take it a step further and look at it from a different perspective. There are two different cases: Code 09 implements the expression stComplex + integer, where stComplex is the left operand; the following code implements the expression integer + stComplex, where the left operand is an integer.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. //+----------------+ 07. private : 08. double m_r, m_i; 09. //+----------------+ 10. public : 11. //+----------------+ 12. stComplex(): m_r(0), m_i(0) {} 13. //+----------------+ 14. stComplex(double r, double i): m_r(r), m_i(i) {} 15. //+----------------+ 16. stComplex operator+(const stComplex &arg1) 17. { 18. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 19. } 20. //+----------------+ 21. stComplex add(const double arg2) 22. { 23. return stComplex(m_r + arg2, m_i); 24. } 25. //+----------------+ 26. void Debug(void) 27. { 28. PrintFormat("Internal Value = %.02f %c %.02fi", m_r, (m_i < 0 ? '-' : '+'), MathAbs(c.m_i)); 29. } 30. //+----------------+ 31. }; 32. //+------------------------------------------------------------------+ 33. void OnStart(void) 34. { 35. stComplex a(2, 5), 36. b(8, -3), 37. c; 38. 39. c = a + b; 40. c.Debug(); 41. 42. c = b.add(4); 43. c.Debug(); 44. } 45. //+------------------------------------------------------------------+
Code 10
Indeed, when we run code 10, we will get the result shown in the following figure.

Figure 05
Note that Code 09 evaluates the expression stComplex + integer, whereas code 10 evaluates the expression integer + stComplex. Although the same types are used in both cases, the operand order differs: in the first case, stComplex is to the left of the + operator, and in the second, it is to the right. For this reason, if we use operator overloading, we must define both signatures separately. That is exactly why I said that this topic is a bit more complex and confusing, although there is nothing here that cannot be explained and put into practice.
So, we have two operand orders that we need to support: stComplex + integer and integer + stComplex. What is the easiest way to support both options? The simplest solution is implemented in code 11, which is shown in full below.
01. //+------------------------------------------------------------------+ 02. #property copyright "Daniel Jose" 03. //+------------------------------------------------------------------+ 04. struct stComplex 05. { 06. //+----------------+ 07. private : 08. double m_r, m_i; 09. //+----------------+ 10. public : 11. //+----------------+ 12. stComplex(): m_r(0), m_i(0) {} 13. //+----------------+ 14. stComplex(double r, double i): m_r(r), m_i(i) {} 15. //+----------------+ 16. stComplex operator+(const stComplex &arg1) 17. { 18. return stComplex(m_r + arg1.m_r, m_i + arg1.m_i); 19. } 20. //+----------------+ 21. stComplex operator+(const double arg2) 22. { 23. return stComplex(m_r + arg2, m_i); 24. } 25. //+----------------+ 26. stComplex operator+=(const double arg2) 27. { 28. return stComplex(m_r += arg2, m_i); 29. } 30. //+----------------+ 31. void Debug(void) 32. { 33. PrintFormat("Internal Value = %.02f %c %.02fi", m_r, (m_i < 0 ? '-' : '+'), MathAbs(m_i)); 34. } 35. //+----------------+ 36. }; 37. //+------------------------------------------------------------------+ 38. void OnStart(void) 39. { 40. stComplex a(2, 5), 41. b(8, -3), 42. c; 43. 44. c = a + b; 45. c.Debug(); 46. 47. (c += 4).Debug(); 48. c = b + 4; 49. c.Debug(); 50. } 51. //+------------------------------------------------------------------+
Code 11
In this case, I modified the code to introduce another aspect. However, operator overloading remains the main topic. When you run code 11, you will get the result shown in the following figure.

Figure 06
With this implementation, we have covered both operand orders in a fairly simple and engaging way: stComplex + integer, as used in Code 09, and integer + stComplex, as used in code 10. However, what we have covered here represents only the very basics of what we need to understand before moving on to the next articles.
Concluding Remarks
In this article, we have seen how to understand and use operator overloading. We have only covered the simplest part of the entire process here, though; we will explore it in greater detail in subsequent articles. With time and practice, you will see, dear reader, that this tool — which is very rarely used in MQL5 — can open up a world of possibilities for you. This is because, when used correctly, operator overloading allows you to write code that is much more readable and understandable. That is exactly why the code becomes safer, more stable, and easier to improve. In addition, this, of course, allows us to better control what we are actually doing.
However, I would like to remind you of something, dear reader. If you happened to land on this article first and are just starting to learn MQL5 programming, I recommend reading and studying the material from the previous articles. Each one contains a small portion of the knowledge you will need to understand what we will be covering in the following articles. In any case, be sure to study and practice what is explained here using the files included in the attachment. In the next article, we will continue to delve deeper into operator overloading, because the fun is just getting started.
| MQ5 File | Description |
|---|---|
| Code 01 | Basic demonstration |
| Code 02 | Basic demonstration |
| Code 03 | Basic demonstration |
| Code 04 | Basic demonstration |
| Code 05 | Basic demonstration |
| Code 06 | Basic demonstration |
| Code 07 | Basic demonstration |
| Code 08 | Basic demonstration |
Translated from Portuguese by MetaQuotes Ltd.
Original article: /pt/articles/16854
Warning: All rights to these materials are reserved by MetaQuotes Ltd. Copying or reprinting of these materials in whole or in part is prohibited.
This article was written by a user of the site and reflects their personal views. MetaQuotes Ltd is not responsible for the accuracy of the information presented, nor for any consequences resulting from the use of the solutions, strategies or recommendations described.