Statements overloading in C ++

перегрузка операторов c++, перегрузка операторов с++, overload c ++ operations, перегрузкoperator overloading c ++

Operator overloading in some extent, the creative process. With her help, programmers it is possible to make it easier for yourself to write code, and for other – improve readability. For example, when some action has to be repeated many times in the code, and just painfully constantly use the special functions – you can overload an operator for this. Let's, in the code it is necessary to often combine strings (append a string to a string class element). It can be designed in different ways. But we will do so, to strings concatenation happened then, when we use the statement + :

The result should be a change in cell str. He will in itself both strings: “The name of our website PureCodeCpp”.

While we do not explicitly override statement +, the compiler will “swear”, so as we put no numbers, and the strings. But now we learn how to tell him, how to act, When we ask to perform non-standard action, using +.

Getting practice. Define in example 4 strings. Next, collect one anecdote, combining them in the correct order in one string.

Let's not talk about the obvious – proceed to reception of the overload. In string 15 we see a prototype of a class method Overload: void statement +(char*); This method will cause the statement to work + so, that we want (as we define below). To overload any statement you want to use the keyword statement. In our case, method does not return the values ​​so void, Further keyword statement and the statement itself +. It adopts this method on a string pointer.

In strings 20 – 23 it is the definition of the method of statement overloading. (How to define a class method is described in the article Classes in C ++) In him, using function strcat_s(str, s); write string s end of the string str (class element). It works so – as soon as the code meets the statement + which will be located behind the string – method is called statement overloading and this string will be transferred to it at the sign.

In the main function we defined in random order 4 strings. Display them on the screen (strings 39 – 42). Below, in string 44, declared object Joke. At the time of its creation, the class constructor, cleanse class element str from “garbage” and it will be ready to record strings. Just follow these simple steps (strings 46 – 49) – using overloaded + write all strings into one (str) in the right order.


перегрузка операторов c++,  operator overloading c ++

Everything turned out. And, as seen in the result, for numeric data statement + load correctly. Therefore you can calmly apply it for arithmetic operations in code – the compiler you “understand”. Another point – overload statement acts only within the class, for which it is defined. If we define another class (Overload2 for example) but not overwhelm him for the statement, the attempt to use + to write a string somewhere will result in an error.

There are a number of exceptions in c++ – not all statements can be overloaded. Here is a list:

перегрузка операторов c++,  operator overloading c ++

And a little theory:

– overload statements can not change the priority and order of operations;

– there is no possibility, using overload, create new symbols for operations;

– binary statements cannot be used to override the unary operation, and vice versa – unary not override binary operation.

Statement overloading, of course, “thing” interesting. Just do not get carried away. Use it only when absolutely necessary – if it will really bring more convenience, saving you time and positively affect the code readability. Try to overload statements so, it to be as close as possible to their logical value. That is, it is not necessary to fold strings, statement overloading , for example. It is more logical to use +. I note, that many programmers do not like statement overloading, as some overly fond of it, and it becomes difficult to read the code. So carefully weigh the pros and cons, when deciding on overloading statements.

In the future I plan to write another article on overloading statements, where the examples I want to show how to overload ++ increment, decrement, == equality, = assignment new and delete. If I could not forget :)

There are videos on the topic. Author MyNekis.

To support our site – click on the piggy bank and choose any convenient way.

Newsletter of programming:

Agree to receive notifications from to my e-mail

Statements overloading in C ++

One thought on “Statements overloading in C ++

  1. >> For example, when some action has to be repeated many times in the code, and just painfully permanent use for this special function - can overload statement for this.

    In general, it is true, but overloading – It is a chic way to shoot yourself in the foot (and even shoot anything comrade). It is a dangerous thing. It can either increase the readability of the code, and all the spoil.

    At Myers were the rules on this subject (if they do not comply with – Hell make you maintain your code all 7 circles). For example:
    – assignment operator should return a reference to * this;
    – in the assignment statement to check self-assignment;
    – there tselyyel about overloading the new and delete, but IMHO 99% mortals simply do not need to touch these statements;
    – declare function, non-members, When type conversion must be applied to all parameters.
    Well, a number of other rules (a few books).
    I suggest to pay attention to the last rule in the list. You have described the statement, who work for

    Joke + str1;
    But it is not going to work for
    str1 + Joke;

    But it would be nice if the statement would work in both directions. It's enough to make it not a member of the class (normal function of). It's not difficult? )

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>