..
Among the distinctive features of C + + language than other programming languages, surely we identify the possibility of extending the use of the mechanism also overloaded operators, in addition to functions or methods of a class.
In a nutshell, the 'overloading is, in general, a mechanism for differentiating functions with the same name (and usually do a similar but not identical) based on the number and type of parameters supplied.
For example, I define two printing functions, a generic (no parameters) that prints a series of predetermined information, and another in which the user can specify by setting parameters appropriately, what information is of interest and therefore to be printed.
The print function is, therefore, in this case redefined to suit the different information provided by the user input. Similarly, the C + + allows you to redefine operators: we think of the mathematical operators "+", "-", "*" and "/". It can be interesting and practical, for example, redefine the "+" operator is applied if not numbers, but strings (sets of alphanumeric characters). In the latter case, the "sum" corresponds to the concatenation of strings, for example if the string is "Hello" and b in the string "world", the operation
string c = a + blead to the creation of the string c, whose value is "Hello world".
'S overloaded, and in particular operator overloading in C + + , are still being treated in a previous article, to which we refer for further investigation.
We now propose to consider, however, specifically how this same mechanism can be extended to other types of operators and how useful, in particular by reference to the new and delete operators, useful for memory management.
The new and delete operators are useful for memory management, in particular to reserve and assign (or allocate) the memory needed for a new object (new) and to free (or deallocate) the memory for future use (delete).
A very interesting aspect of C + +, which distinguishes it from C, it is the existence of new and delete, which allow, for example with regard to new, to calculate the space required for each object automatically (while the command equivalent C, malloc, requires the programmer to specify what is the size of the object to be allocated).
At this point it is natural to wonder what might be the usefulness of a mechanism for overloading, since the strength of new and delete is just a great general use, it seems unnecessary to make a new definition which takes into account a number of cases. The price of an operator is so generic, but, as often happens, a certain lack in control of. For example, it is not possible, using the default version of the new operator, simply check whether there have been problems in allocating the necessary memory (memory is available? Has been allocated correctly?). To overcome this problem one can have recourse to 'overloaded.
So we will discuss now with a simple example, a case of overloading the new operator, stressing that the considerations given and the procedures are valid even if you want to delete from the operator overloading.
| |
Excel (Ebook)
Create spreadsheets and calculation. Just 25 €. |
| |
Webmaster Advanced (Course)
Become a professional Webmaster. From 39 €. |
| |
XML (Course)
Creation of XML structures, XSL and other languages extensible. Starting from 29 €. |